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:
Install as NuGet package Newtonsoft Json, in order to use its
JsonProperty
attribute.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)
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
What Is the Correct Way to Compare Char Ignoring Case
Adding an Incremental Number to Duplicate String
C# Json Serialization of Dictionary into {Key:Value, ...} Instead of {Key:Key, Value:Value, ...}
C# How to Simply Encrypt a Text File With a Pgp Public Key
Choosing the Default Value of an Enum Type Without Having to Change Values
C# ASP.NET MVC Download a File Via Ajax
How to Save Mailmessage Object to Disk as *.Eml or *.Msg File
How to Set the Query Timeout from SQL Connection String
Asp.Net Core API Post Parameter Is Always Null
How to Get Group of Records With Latest Date Using Linq to Entities
How to Fix Json.Net (Newtonsoft.Json) Runtime File Load Exception
How to Set Autoincrement Ids in Unit Test
How to Have a a Razor Action Link Open in a New Tab
Select Distinct by Two Properties in a List
How to Print the Elements With Text Value That Contains in a List Selenium C#