Get Request Content of PUT method in WCF - c#

I am trying to fetch "PUT" request content which is VCard via WCF.
here is its content,
PUT https://mysite.com/
Host: mysite.com
Content-Type: text/vcard; charset=utf-8
Accept-Encoding: gzip, deflate
Connection: keep-alive
Proxy-Connection: keep-alive
Accept: */*
Content-Length: 280
Accept-Language: en-us
BEGIN:VCARD
VERSION:3.0
N:test;Syeda;;;
FN:Syeda test
item1.EMAIL;type=INTERNET;type=pref:test#hotmail.com
TEL;type=HOME;type=VOICE;type=pref:021 3123456
REV:2013-12-09T13:51:45Z
UID:318D8B19-BBC0-4841-A831-89E224C27A2D
END:VCARD
when i am trying to fetch it it is giving the following error,
Expecting element 'string' from namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element' with name 'Binary', namespace ''.
Is there any workaroung anybody knows to access VCard content.
Though, i am able to get those request's content that is in Xml format.
Thanks!
Update:
Previously, i was fetching the request content using this line of code.
operationContext.RequestContext.RequestMessage.ToString()
This work fine until the request is in xml format.But, when request is bringing content given above it returns word "Stream" as a string.
And, when i tried to do something like this,
operationContext.RequestContext.RequestMessage.GetBody<String>();
I am getting error shown above.

Related

Parse an content-type header from http request stream

I have the contents of an http request saved on disk.
It looks like this:
POST http://test.ca/ 1.1
Accept: application/xml
Content-Type: multipart/form-data; boundary="8942d141-7753-45ab-be3a-53021694e70c"
--8942d141-7753-45ab-be3a-53021694e70c
Content-Type: application/xml Content-Disposition: form-data; name=Bundle
...
--8942d141-7753-45ab-be3a-53021694e70c
Content-Type: image/jpg Content-Disposition: form-data; name="image 1 of 2"; filename=Attachment1.jpg; filename*=utf-8''Attachment1.jpg
...
--8942d141-7753-45ab-be3a-53021694e70c--
Normally, if you were processing this at the time of request, the HttpRequest object would be used to get the content type. If I have that request persisted to disk, are there any available classes I can use to handle parsing the ContentType for me?
Everything else can be parsed using the MultipartReader class, but I'm not aware of anything that will handle parsing the request header, given an instance of a stream. If I don't have to roll my own that would be great.

Uploading multipart form files with HttpClient

I am trying to create an attachment using the Support Bee API as documented here:
https://supportbee.com/api#create_attachment
I have written a service that uses an HttpClient to create and send the request using a filename.
If I test in in Postman, it succeeds. I am using form-data for the body and just selecting the file to upload from the UI:
It doesn't work when I try to upload it via my HttpClient Service:
public async Task<string> CreateAttachmentAsync(string fileName)
{
// "client" is HttpClient provided via D.I.
MultipartFormDataContent content = new MultipartFormDataContent();
content.Add(new StreamContent(new FileStream(fileName, FileMode.Open)), "files[]");
using (HttpResponseMessage response = await client.PostAsync(
"https://xxx.supportbee.com/attachments?auth_token=xxx",
content))
{
string responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
}
This results in a 500 Internal Server Error. Inspecting the MultipartFormDataContent object I can see that it's header values are automatically being set:
{
Content-Type: multipart/form-data; boundary="c9be3778-4de5-4460-9929-adcaa6bdda79"
Content-Length: 164
}
I have also tried reading the file to a byte array first and using ByteArrayContent instead of StreamContent to no avail. The response doesn't provide anything helpful, but since the request works in Postman I must have something wrong with my code, but I don't know what else to try.
Edit: I tested with Fiddler to compare the successful Postman request to my code. Here is the request with Postman:
POST
https://xxx.supportbee.com/attachments?auth_token=xxx
HTTP/1.1 User-Agent: PostmanRuntime/7.22.0 Accept: / Cache-Control:
no-cache Postman-Token: f84d22fa-b4b1-4bf5-b183-916a786c6385 Host:
xx.supportbee.com Content-Type: multipart/form-data;
boundary=--------------------------714700821471353664787346
Accept-Encoding: gzip, deflate, br Content-Length: 241 Connection:
close
----------------------------714700821471353664787346 Content-Disposition: form-data; name="files[]"; filename="sample.txt"
Content-Type: text/plain
This contains example text.
----------------------------714700821471353664787346--
And the failing request from my code:
POST
https://xxx.supportbee.com/attachments?auth_token=xxx
HTTP/1.1 Host: xxx.supportbee.com Accept: / Accept-Encoding:
gzip, deflate, br Connection: close Content-Type: multipart/form-data;
boundary="ea97cbc1-70ea-4cc4-9801-09f5feffc763" Content-Length: 206
--ea97cbc1-70ea-4cc4-9801-09f5feffc763 Content-Disposition: form-data; name="files[]"; filename=sample; filename*=utf-8''sample
This contains example text.
--ea97cbc1-70ea-4cc4-9801-09f5feffc763--
The difference I can see is that the individual part in Postman has its own Content-Type: text/plain header for the file, and mine doesn't. I'm unable to add this because if I try content.Headers.Add("Content-Type", "text/plain"); It fails with 'Cannot add value because header 'Content-Type' does not support multiple values.'
First, it's important to note that a 500 response is akin to an unhandled exception, i.e. it's a bug on their end and more or less impossible to know for sure what you did wrong. I would suggest reporting it to them and, although I'm not familiar with Support Bee, I would hope they have good support people who can help you troubleshoot. :)
But if you want to play the guessing game, I agree that subtle differences between your successful Postman call and your code are a good place to start. For that header, note that content is the MultipartFormDataContent. You actually want to set it on the StreamContent object.
Also, look at the request headers Postman is sending and see if Content-Disposition includes a filename. You might need to add that to your code too, if the API is expecting it.
Here's how to do both:
var fileContent = new StreamContent(File.OpenRead(path));
fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
content.Add(fileContent, "files[]", Path.GetFileName(path));
If that's not the problem, look at the "raw" version of the request body in Postman, as well as those 11 request headers, and see if you can spot anything else you might be missing.

ASP.NET WebAPI - Issues with [FromBody]

I'm looking to implement a new API but rather than going down my usual route of PHP I've opted for C#.
I've had a little play with the 'MVC 4 API' example, but so far I can't seem to get my requests working.
I've created a controller called 'Values' and have the following method:
[HttpPost]
public void Post([FromBody]string value)
{
string body = value;
}
I've then sent the following POST request via fiddler to said API:
User-Agent: Fiddler
Host: localhost:56501
Content-Length: 10
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
value=test
After running this, it looks as though the method is being called, but when attempting to get the 'value' sent through the body, it doesn't seem to pick up on it and instead gives me a null.
I can't seem to make it work for Content-Type: application/x-www-form-urlencoded, but this should work meanwhile :)
User-Agent: Fiddler
Host: localhost:56501
Content-Length: 6
Content-Type: application/json; charset=UTF-8
"test"
So I found out that if you need to make it work with application/x-www-form-urlencoded then you only need to pass =value as the body of your request. Definitely weird, but I guess this is not MVC, it's WebApi, so you shouldn't be passing application/x-www-form-urlencoded anyway ;)

Send multiple of items to WCF Dataservice at once?

I'm using WCF Data Service as a way to allow other webservice in the project to connect to the database. My problem is that I our project has a crawler that add tens of items to the database every hour.
Using AddToItems method (which is auto generated by ADO.NET) leads to timeout exception or at least it makes the crawler in need to wait for a lot of time taking into consideration that Addto method handles each independently.
*Notes :
1- I've added an interceptor on adding to items to perform some actions when a new item is added.
2- WCF Data services Service Operations doesn't allow taking parameters of a user defined data type , that prevented me from creating a service operation that takes a list of items as a parameter to be able to handle multiple items at each time and at the same way to allow the client to handle it asynchronously.
When I tried to serialize this list so it can be treated as a string , an exception has occurred because of the length limit of the url even when POST is used instead of Get.
Update : Saveing Changes via BeginSaveChanged and EndSaveChanged solved the problem to some extent but I'm still looking for a better solution
Maybe the OData feature Batch can address your requirement:
var client = new Container(serviceUrl);
client.Format.UseJson();
DefaultBatchCustomer customer0ToAdd = new DefaultBatchCustomer { Id = 10, Name = "Customer 10" };
DefaultBatchCustomer customer0ToAdd = new DefaultBatchCustomer { Id = 11, Name = "Customer 11" };
client.AddToDefaultBatchCustomer(customer0ToAdd);
client.AddToDefaultBatchCustomer(customer1ToAdd);
var response = await client.SaveChangesAsync(SaveChangesOptions.BatchWithSingleChangeset);
and the request looks like:
POST http://jinfutanwebapi1:9123/DefaultBatch/$batch HTTP/1.1
OData-Version: 4.0;NetFx
OData-MaxVersion: 4.0;NetFx
Content-Type: multipart/mixed; boundary=batch_8ce61768-e8bb-4117-954b-9bc43e05baef
Accept: multipart/mixed
Accept-Charset: UTF-8
User-Agent: Microsoft ADO.NET Data Services
Host: jinfutanwebapi1:9123
Content-Length: 1772
Expect: 100-continue
--batch_8ce61768-e8bb-4117-954b-9bc43e05baef
Content-Type: multipart/mixed; boundary=changeset_b36bec94-fc3b-4d89-99cc-0610fcec8148
--changeset_b36bec94-fc3b-4d89-99cc-0610fcec8148
Content-Type: application/http
Content-Transfer-Encoding: binary
POST http://jinfutanwebapi1:9123/DefaultBatch/DefaultBatchCustomer HTTP/1.1
Content-ID: 13
OData-Version: 4.0;NetFx
OData-MaxVersion: 4.0;NetFx
Content-Type: application/json;odata.metadata=minimal
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
User-Agent: Microsoft ADO.NET Data Services
{"#odata.type":"#WebStack.QA.Test.OData.Batch.Tests.DataServicesClient.DefaultBatchCustomer","Id":10,"Name":"Customer 10"}
--changeset_b36bec94-fc3b-4d89-99cc-0610fcec8148
Content-Type: application/http
Content-Transfer-Encoding: binary
POST http://jinfutanwebapi1:9123/DefaultBatch/DefaultBatchCustomer HTTP/1.1
Content-ID: 14
OData-Version: 4.0;NetFx
OData-MaxVersion: 4.0;NetFx
Content-Type: application/json;odata.metadata=minimal
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
User-Agent: Microsoft ADO.NET Data Services
{"#odata.type":"#WebStack.QA.Test.OData.Batch.Tests.DataServicesClient.DefaultBatchCustomer","Id":11,"Name":"Customer 11"}
--changeset_b36bec94-fc3b-4d89-99cc-0610fcec8148--
--batch_8ce61768-e8bb-4117-954b-9bc43e05baef--

FromBody not binding string parameter

I have an issue similar to ASP.NET MVC 4 RC Web API Parameter Binding Issue, but I'm trying to solve it by using the [FromBody] attribute.
Fiddler reports the following request (excluding irrelevant bits like User Agent String)
PUT http://localhost:82/api/account/shoppinglistitems HTTP/1.1
Host: localhost:82
Connection: keep-alive
Content-Length: 11
Origin: http://localhost:3000
Content-Type: application/x-www-form-urlencoded
Accept: application/json, text/javascript, */*; q=0.01
query=apple
My controller action is
[HttpPut]
public ShoppingListItemWebModel CreateShoppingListItem([FromBody]string query) {
// query is null
}
I could wrap the parameter in a complex type, but that seems like a hack to fix the issue. Or I could put the query in the URI, but that doesn't fit the pattern of the rest of the API. Is it possible to do it this way? If so, where is my mistake?
change your request to be
PUT http://localhost:82/api/account/shoppinglistitems HTTP/1.1
Host: localhost:82
Connection: keep-alive
Content-Length: 11
Origin: http://localhost:3000
Content-Type: application/x-www-form-urlencoded
Accept: application/json, text/javascript, */*; q=0.01
=apple
notice the lack of "query"
If you're using AngularJS instead of jQuery you might want to opt for the following alternative, since AngularJS uses content type application/json by default.
PUT http://localhost:82/api/account/shoppinglistitems HTTP/1.1
Host: localhost:82
Connection: keep-alive
Content-Length: 7
Origin: http://localhost:3000
Content-Type: application/json
Accept: application/json, text/javascript, */*; q=0.01
'apple'
Notice that the value is wrapped in string quotes (either single or double).

Categories

Resources