Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST error-The request contain entity body but no Content-Type header.The infered media type application/octet-stream is not support for this resource

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?

like image 632
user7768692 Avatar asked Oct 18 '25 01:10

user7768692


2 Answers

Try to write:

Content-Type: application/json; charset=UTF-8"

Or add to your header:

Accept: application/json
like image 70
Mahsa Hassankashi Avatar answered Oct 20 '25 14:10

Mahsa Hassankashi


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);
like image 37
Nkosi Avatar answered Oct 20 '25 16:10

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!