Problems with POST using JSON in the request body

30,470 views
Skip to first unread message

zythra

unread,
Jun 14, 2011, 4:37:11 PM6/14/11
to RestSharp
I'm trying to do a POST passing JSON in the request body.

Here's how I'm setting it up:

RestRequest request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddParameter("UserName", "username");
request.AddParameter("Password", "password");

Some other variations I've tried are:

request.AddBody(new { UserName = "username", Password = "password" });

request.AddParameter("values", "{\"Password\":\"username\",\"UserName
\":\"password\"}", ParameterType.RequestBody);

request.AddParameter("application/json", "{\"Password\":\"username\",
\"UserName\":\"password\"}", ParameterType.RequestBody);

I've also tried adding the following parameter with the different
variations:

request.AddHeader("Content-Type", "application/json");

No matter the variation I always get the following error:

"The incoming message has an unexpected message format 'Raw'. The
expected message formats for the operation are 'Xml', 'Json'."

I've gotten other calls working with RestSharp that don't take JSON in
the body, but I can't get this one to work. I can also get this one
to work in Firefox using the REST client extension.

I've been trying to get this to work for several days now. I really
like the simplicity of RestSharp, but if I can't get this to work I
may have to use something else which I'd rather not do.

Any ideas?

Thanks!

John Sheehan

unread,
Jun 15, 2011, 7:26:16 PM6/15/11
to rest...@googlegroups.com
What's the content type the server is expecting? If it's
application/json then AddBody is the right one. If not, you need to
use this hacky workaround

request.AddParameter("text/json", "body content as a string here",
ParameterType.RequestBody);

zythra

unread,
Jun 15, 2011, 7:37:44 PM6/15/11
to RestSharp
Thanks for the reply John. It's expecting application/json. Using
the AddBody method isn't working and I just tried the work around with
text/json and got the same error back.

Unfortunately I'm not sure where to go from here other than using
something else. I've got all of my calls working with RestSharp
except for this one, which is the only only that takes json in the
request body.

I'm going to talk to the guy that wrote the service I'm consuming and
see if maybe he's got an idea. Maybe something is misconfigured on my
service.

zythra

unread,
Jun 16, 2011, 12:02:36 PM6/16/11
to RestSharp
I figured it out. I thought I'd post my findings in case they might
help someone else in the future.

Basically I was adding a body to my request using:

request.AddBody(new { UserName = username", Password = "pass" });

Which is the correct way to do it. What was making it break was the
fact that I was also adding a parameter using:

request.AddParameter("apikey",
"ba54b9a8-75ab-4dca-8a87-5e1863ba0482");

What I found in the documentation (https://github.com/johnsheehan/
RestSharp/wiki/ParameterTypes-for-RestRequest - see the last
paragraph at the bottom of the page) is that by adding that parameter,
since my request is a post, it was overriding the body that I had
added to the request. I don't know why this is the behavior, I can
only assume there is a good reason for it. Maybe John can shed some
light on that.

Anyway, I'm very happy to have been able to work through this.
RestSharp now meets all of my requirements that I have thrown at it so
far.

John Sheehan

unread,
Jun 16, 2011, 12:11:08 PM6/16/11
to rest...@googlegroups.com
Where is the apikey parameter supposed to go? In the URL or as a POST parameter?

If it's a GET request, you can't have a request body and AddParameter
adds values to the URL querystring. If it's a POST you can't include a
POST parameter and a serialized request body since they occupy the
same space. You could do a multipart POST body but this is not very
common. Unfortunately if you're making a POST the only way to set the
URL querystring value is through either string concatenation or
UrlSegments:

var key = "12345";
var request = new RestRequest("api?key=" + key);
// or
var request = new RestRequest("api?key={key});
request.AddUrlSegment("key", "12345");

Bryan Roth

unread,
Mar 9, 2012, 12:21:32 PM3/9/12
to rest...@googlegroups.com
Sorry to resurrect this old thread.

If I understand you correctly, you can use UrlSegments and a request body in a single POST call? Below is the code that I'm trying to get to work.

var request = new RestRequest();
request.Resource = "Items/Item?sessionID={sessionID}&developerKey={developerKey}";                       
request.Method = Method.POST;
request.RequestFormat = DataFormat.Json;

request.AddBody(request.JsonSerializer.Serialize(item));            

return Execute<Record>(request);

Bryan Roth

unread,
Mar 9, 2012, 1:06:33 PM3/9/12
to rest...@googlegroups.com
I solved the problem by passing the object without serializing it myself:

request.AddBody(item);

Jafin

unread,
Mar 18, 2012, 1:25:21 AM3/18/12
to rest...@googlegroups.com
Smack! just hit the same issue. Did not expect addBody to reserialize my json body.
Found this as a workaround if dealing with an alredy serialized body http://stackoverflow.com/questions/5095692/how-to-add-text-to-request-body-in-restsharp

jack chen

unread,
Aug 16, 2012, 4:45:53 PM8/16/12
to rest...@googlegroups.com
Yeah, I also found the stackoverflow link and it fixed my problem as well.

the request.AddBody for JSON added two double quotation in the body, also it doesn't support nested json object like {"fields":{"customfield_10470":"54321"}} 

Better do the serialization yourself.

John Tabone

unread,
Jun 26, 2013, 11:48:22 AM6/26/13
to rest...@googlegroups.com, evo...@gmail.com
I know this was posted a while ago, but what kind of object was item?

Raghu Kandru

unread,
Nov 26, 2013, 8:07:53 AM11/26/13
to rest...@googlegroups.com, dus...@zythra.com
why don't you use HttpClient,and HttpPost class for posting your data to api
if u want to implement that use this:

for this you need org.apache.http package and jasonsimple jar file

HttpClient c = new DefaultHttpClient();
HttpPost p = new HttpPost("your url");   //if u have secret keys u can include it in your url(Ex:http://www.yourdomain.com/createaccount?secretkey="774577");
 p.setHeader("ContentType", "application/json");
JSONObject jsonObject = new JSONObject();/
 jsonObject.put("UserName", username);
 jsonObject.put("Password", password);
 p.setEntity(new StringEntity(jsonObject.toString(), "UTF-8"));          
 HttpResponse r = c.execute(p);                       


if this is not related to your scenario please ignore it

-----------------------------------------------------------------
Reply all
Reply to author
Forward
0 new messages