When I add ContentType header to request it becomes null instead of application/json. I can see it in the debugger. Here is the code
RestClient client = new RestClient(url);
client.Timeout = -1;
RestRequest request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer " + token);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", requestBody, ParameterType.RequestBody);
IRestResponse<T> response = await client.ExecuteAsync<T>(request);
Debugger Image
Any suggestions ? Thanks in advance
I tried changing the “Content-Type” to “ContentType” but that doesn’t work
Perturbator is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
0
Looking through the RestSharp request documentation it seems that you do not need to set the content type manually it will be set automatically.
Request Body With AddParamter
In regards to adding a request body, the documentation points out:
We recommend using
AddJsonBody
orAddXmlBody
methods instead of
AddParameter
with typeBodyParameter
.
Those methods will set the
proper request type and do the serialization work for you.
Request Body With AddJsonBody
In regards to using AddJsonBody
, the documentation points out:
When you call
AddJsonBody
, it does the following for you:
- Instructs the RestClient to serialize the object parameter as JSON
when making a request- Sets the content type to
application/json
- Sets the internal data type of the request body to DataType.Json
Suggested Fix
- Remove
AddHeader
for contentType - Replace
AddParamter
withAddJsonBody
This will set the content type in the request as expected.
//request.AddHeader("Content-Type", "application/json");
//request.AddParameter("application/json", requestBody, ParameterType.RequestBody);
request.AddJsonBody(requestBody);
Working Example
See dotnedFiddle sample application here