Tuesday, February 21, 2012

RestSharp and Advanced POST Requests

I have learned much about how RestSharp handles POST requests as of version 102. If you provide parameters of the GetOrPost type, by default the post mechanism places these parameters
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:

coldplay said...

Thank you, very helpful !

Aditi said...

Really thanks a lot!! It worked for me :)

Aditi said...

Thanks a lot it worked for me :)1

Anonymous said...

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...

Anonymous said...

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

Carsten said...

This approach does not seem to work with PUT requests. Any idea?

Bryan Wood said...

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.

Danilo Oliveira said...

You, Sir ,are awesome!

Bryan Wood said...

@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. :)

Madhangi said...

WOw.. Thank you.. I have spent a day and half on other solutions.. You have really found and fixed the problem.. Thanks!

Unknown said...

Thank you very much for posting the fix! Definitely works now.

Unknown said...

This has solved days of troubleshooting! THANK YOU!!!!

Ajax Lesson

NOTE : This lesson uses jQuery as the interface for basic AJAX work. By no means is AJAX a product of jQuery and you do not need jQuery to ...