4

I'm working on this project that needs to serialize JSON objects to post parameters using RestSharp, below is my code:

        var request = new RestRequest();
        request.Method = Method.POST;
        request.RequestFormat = DataFormat.Json;

        request.AddBody(jsonObject);
        return client.Execute<dynamic>(request);

What I realize is instead of adding each JSON name value pair as a post parameter, request.AddBody adds the whole JSON string as one large post parameter. My question is, is there any way to cause request.AddBody method to add each JSON name-value pair as individual post parameters? I know that request.AddParameter() does the job but that requires manual effort to add each parameter.

Instead of:

     [0]:{
           application/json="
           {
               "name":"john doe",
               "age": "12",
               "gender": "male"}
           }
         }

Desired Result:

     [0]:"name":"john doe"
     [1]:"age":"12"
     [2]:"gender":"male"
3
  • Can you give an example of what you're trying to accomplish? Show us what you're starting with and what you want the end result to be. Commented Jan 10, 2014 at 3:43
  • 1
    I don't understand the format of your "desired result" - it doesn't seem like a standard format for the body of a POST request. Something like ?name=john doe&age=12&gender=male would be more normative. Can you just confirm if that is the kind of thing you had in mind? Commented Jan 10, 2014 at 11:13
  • @DanielC, yes, it is exactly what i want. but instead of having a foreach loop to add the individual parameter, i was just wondering is there any solution in JSON.net which can convert the all the json into something like ?name=john doe&age=12&gender=male Commented Jan 11, 2014 at 3:39

3 Answers 3

5
request.AddObject(jsonObject)

should do what you expect

Quote from RestSharp docs:

To add all properties for an object as parameters, use AddObject().

Sign up to request clarification or add additional context in comments.

2 Comments

You should include why this should do as expected. This is a weak answer without an explanation.
That's basically a replacement for loop above, doing AddParameter() for each field inside the jsonObject.
2

Had a similar question and found this thread. Came up with a simple approach that I thought I would share in case it would help others.

The trick is to use anonymous objects along with AddJsonBody().

request.AddJsonBody(new { name = "john doe", age = "12", gender = "male" });

RestSharp will automatically serialize the anonymous object into the desired JSON...

{
    "name":"john doe",
    "age": "12",
    "gender": "male"
}

Comments

1

The answer would seem to be to iterate through your jsonObject and turn each desired JSON name-value pair into a parameter. To do this you can use the request.AddParameter method in a loop which iterates through the name-value pairs of your jsonObject with something like:

foreach (var pair in jsonObject) 
{ 
    request.AddParameter(pair.Key, pair.Value); 
}

This is probably oversimplified, but using a library like JSON.NET, it should be quite easy to do. Then you can wrap this functionality into a nice little method somewhere and reuse at will. No manual labour.

NB: You probably want to remove the line request.RequestFormat = DataFormat.Json in your existing code, since JSON is exactly what you don't appear to want to POST.

1 Comment

thanks daniel, i guess this would be the only solution for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.