问题
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