Restsharp Serializing Json Objects to Post Parameters

RestSharp Serializing Json objects to Post parameters

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.

RestSharp Serialize JSON Array to request parameter

Not very slick, but this would work:

var request = new RestSharp.RestRequest();

var locations = new Dictionary<string, object>();
locations.Add("A", 1);
locations.Add("B", 2);
locations.Add("C", 3);

JsonObject o = new JsonObject();

foreach (var kvp in locations)
{
o.Add(kvp);
}

JsonArray arr = new JsonArray();
arr.Add(o);

request.AddParameter("locations", arr.ToString());
request.AddParameter("date", 1434986731000);

AddBody for RestSharp is failing when serializing to JSON

You can specify that the body parameter is JSON by doing the following:

request.AddParameter("application/json", jsonString, ParameterType.RequestBody);

Your case:

restRequest.AddParameter("application/json", 
JsonConvert.SerializeObject(requestObj, this.jsonSettings),
ParameterType.RequestBody);

Object to JSON issue in RestSharp

Since RestSharp uses as default Json serializer SimpleJson that, for what I know, doesn't support attributes to change serialization behaviour, I would:

  1. Install as NuGet package Newtonsoft Json, in order to use its JsonProperty attribute.

  2. Define a class to provide an authentication object like this:

    public class AuthenticationInfos
    {
    [JsonProperty(PropertyName = "username")]
    public string Username { get; set; }

    [JsonProperty(PropertyName = "password")]
    public string Password { get; set; }

    [JsonProperty(PropertyName = "Api-Key")]
    public string ApiKey { get; set; }
    }

(The key part is here [JsonProperty(PropertyName = "Api-Key")], you are telling the serializer to serialize that property with that name, which is invalid as a C# variable)


  1. Use Newtonsoft Json serialization to serialize the body request:

    var client = new RestClient("https://TestWeb");

    var request = new RestRequest("login", Method.POST);
    request.AddHeader("Content-type", "application/json");

    var body = new
    {
    credentials =
    new AuthenticationInfos()
    {
    Username = "Uname",
    Password = "password",
    ApiKey = "myApiKey"
    }
    };

    var serializedBody = JsonConvert.SerializeObject(body);
    request.AddParameter("application/json", serializedBody, ParameterType.RequestBody);

The body will be like:

{
"credentials":
{
"username": "Uname",
"password": "password",
"Api-Key": "myApiKey"
}
}

Of course you'll have to the same in deserialization phase when receiving the message.



Related Topics



Leave a reply



Submit