Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Invalid JSON String' for POST request with application/json content type #851

Closed
dhrushilbadani opened this issue May 26, 2016 · 5 comments

Comments

@dhrushilbadani
Copy link

dhrushilbadani commented May 26, 2016

So I was making a POST request to an endpoint that needs application\json. I initialized the RestClient and request and used
request.AddHeader("Content-Type","application/json")
and made subsequent calls to request.AddParameter() but the error I get is Invalid JSON String with a status code of UnsupportedMediaType and also mentions expected application/json or text/json body. Any idea of what's going on?

I tried using a custom serializer as well, but to no avail. The request works successfully on Chrome's Advanced Rest Client.

@behoyh
Copy link

behoyh commented May 26, 2016

I was not able to replicate this issue. Do you have any more information on what you were trying to do?

@dhrushilbadani
Copy link
Author

dhrushilbadani commented May 27, 2016

So, I found the workaround -- apparently request.AddParameter("Content-Type", "application/json") does not work, which meant my request body was not in JSON. http://itanex.blogspot.co.at/2012/02/restsharp-and-advanced-post-requests.html had a solution to this. Is there a more 'standard' way to add the Content-type header?

@Danthar
Copy link

Danthar commented Sep 21, 2016

I have exactly the same problem:

var client = new RestClient(endpoint);

 //client.AddDefaultHeader("Content-Type", "application/json"); //I expect this to be enough
 var req = new RestRequest("api/values");
 req.AddHeader("Content-Type", "application/json"); //but its not so i tried adding it here
 req.AddParameter("application/json", "test", ParameterType.RequestBody); //which didnt work so i tried adding it here
 req.Method = Method.POST;

 var resp = client.Execute(req);

The above described variations of me, trying to send a raw string to a REST api. Do not work.
It does not work, because the RestSharp api does not set the Content-Type header. If i dump the entire request on the server side, it clearly shows that no Content-Type header is set.

I am not sure why this is happening. But it basically bars me from using the library.

@alexeyzimarev
Copy link
Member

Thanks for reporting. I think the request format need to be set more conveniently.

@alexeyzimarev
Copy link
Member

It is actually all fixed. Use AddJsonBody and both the request format and content type will be set for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants