into the body of the request and applies the "application/x-www-form-urlencoded" Content-Type Header. There is no way to override this process and header application, with the simple/basic development pattern, as one would expect.
Package: RestSharp
Version 104.1
HTTP Verb: POST
The goal is to POST a JSON request with a JSON encoded body with a querystring value. This is perfectly fine as the HTTP spec outlines. However, with RestSharp there are some "round about" way to get this result.
Expected Use:
Resulting Header (Reduced to example content):
POST HTTP 1.1 http://www.example.com/where/else Accept: application/json Content-Type: application/x-www-form-urlencoded Content-Length: ## "key"="value"
This was not expected and the endpoint errors out since it was expected JSON and fails to identify and convert the value. After digging deep into the code of RestSharp on GITHUB, I found out why I was getting this. After being frustrated with why this was happening, it took a bit to understand why it was designed the way it was. Once I understood that, I was able to create the correct solution.
Corrected Solution:
Resulting Header (Reduced to example content):
POST HTTP 1.1 http://www.example.com/where/else?key=value Accept: application/json Content-Type: application/json Content-Length: ## strJSONContent
Thanks
- MadZak for beating me over the head so I understood what was going on
- John Sheehan for his work on the RestSharp Library
UPDATE 07/29/2013
I have created a project to maintain the demonstration of this solution. You can access the github repo here. Thanks to all the inquiries on this solution. I have updated this to use 104.1 of RestSharp.
12 comments:
Thank you, very helpful !
Really thanks a lot!! It worked for me :)
Thanks a lot it worked for me :)1
Yes thanks a lot !
This was the key for me :
request.AddParameter("application/json", strJSONContent, ParameterType.RequestBody);
Instead of request.AddBody(strJSONContent) and many other try...
Yes, thanks a lot !
This was the key for me:
request.AddParameter("application/json", strJSONContent, ParameterType.RequestBody);
Instead of many try like:
request.AddBody(myObject);
request.AddBody(new { myObject = myObject });
...
This approach does not seem to work with PUT requests. Any idea?
This should work for all request types though my tests and purpose of this article is POST and RestSharp 1.02. If you are using a newer version of RestSharp this might not be valid for all HTTP Verbs anymore.
You, Sir ,are awesome!
@carsten, I have not worked with PUT and RestSharp lately. I would have to look at how it is handling PUTs specifically, which is how I discovered that it was being funky with POST. That might be a good topic for another blog entry. :)
WOw.. Thank you.. I have spent a day and half on other solutions.. You have really found and fixed the problem.. Thanks!
Thank you very much for posting the fix! Definitely works now.
This has solved days of troubleshooting! THANK YOU!!!!
Post a Comment