Reputation: 76
I'm trying to send POST request. While sending via POSTMAN all goes well, then I try to send it by C# code:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var client = new RestClient(MY-URL);
var request = new RestRequest(Method.POST);
request.Credentials = new System.Net.NetworkCredential(ServerUsername, Password);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("undefined", My JSON Data, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
I'm getting this error:
The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource
How can I solve it?
Upvotes: 4
Views: 23381
Reputation: 364
In my case, I was using Fiddler and somehow I had an extra carriage return that I could not see within Fiddler.
Fiddler SHOWED this in my scratch pad:
POST https://mywebsite/?api-version=7.3 HTTP/1.1
AnotherHeader: 2413OL
But after pasting into Nodepad I saw this:
POST https://mywebsite/?api-version=7.3 HTTP/1.1
AnotherHeader: 2413OL
After removing the extra carriage return in Notepad and then pasting back into Fiddler's scratch pad, all is well and the 415 status code is gone.
Upvotes: 0
Reputation: 2139
Try to write:
Content-Type: application/json; charset=UTF-8"
Or add to your header:
Accept: application/json
Upvotes: 4
Reputation: 247363
Adding a parameter as body changes the content type of the request.
In your example
request.AddParameter("undefined", My JSON Data, ParameterType.RequestBody);
overrides the content type previously set.
If are you serializing an object model to send, then replace request.AddParameter
with
request.AddJsonBody(model);
which will serialize and include the appropriate header information
Otherwise you need to include type when adding the parameter
request.AddParameter("application/json", "My JSON Data", ParameterType.RequestBody);
Upvotes: 2