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

让人想犯罪 __ 提交于 2021-01-04 05:46:37

问题


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?


回答1:


Try to write:

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

Or add to your header:

Accept: application/json



回答2:


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);


来源:https://stackoverflow.com/questions/53515565/rest-error-the-request-contain-entity-body-but-no-content-type-header-the-infere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!