Restsharp JSON Parameter Posting

RestSharp JSON Parameter Posting

You don't have to serialize the body yourself. Just do

request.RequestFormat = DataFormat.Json;
request.AddJsonBody(new { A = "foo", B = "bar" }); // Anonymous type object is converted to Json body

If you just want POST params instead (which would still map to your model and is a lot more efficient since there's no serialization to JSON) do this:

request.AddParameter("A", "foo");
request.AddParameter("B", "bar");

How to add json to RestSharp POST request

I've ran into this problem as well. Try something like this instead of AddJsonBody.

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

RestSharp Post a JSON Object

You need to specify the content-type in the header:

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

Also AddParameter adds to POST or URL querystring based on Method

I think you need to add it to the body like this:

request.AddJsonBody(
new
{
UserName = "UAT1206252627",
SecurityQuestion = securityQuestion
}); // AddJsonBody serializes the object automatically

RestSharp POST Object as JSON

There are a couple of issues I can see.

The first is that the object you're sending has no public fields, I'd also simplify the definition a little too:

public class PTList
{
public PTList() { get; set; }
}

The second issue is that you're setting the Content-Type header which RestSharp will do by setting request.RequestFormat = DataFormat.Json

I'd also be tempted to use generics rather than an Object

Your httpPost method would then become:

protected static IRestResponse httpPost<TBody>(String Uri, TBody Data)
where TBody : class, new
{
var client = new RestClient(baseURL);
client.AddDefaultHeader("X-Authentication", AuthenticationManager.getAuthentication());
var request = new RestRequest(Uri, Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(Data);

var response = client.Execute(request);
return response;
}

How to send json Data using RestSharp POST Method in c#

in your web api you need to handle the binding json data. WebApi does it by itself normally.
And also you dont have to add header or any parameter with "application/json".
If you could just simply use AddJsonBody(object obj) method RestSharp will automatically set the header.

Or another way to do that is:

    Random r = new Random((int)DateTime.Now.Ticks);
var x = r.Next(100000, 999999);
string s = x.ToString("000000");
string UniqueFileName = "S" + s + DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
request.Resource = "api/BFormat/AddNewBFormat";
request.Method = Method.POST;
request.RequestFormat = DataFormat.Json;
var body = new
{
UploadFileVM = new
{
BordereauxId = "",
BFormatId = "",
FileName = UniqueFileName,
Filesize = 0,
Path = @"c:\sdakdldas\"
}
};
request.AddBody(body); //enter code herE
var queryResult = client.Execute<ResponseData<Guid>>(request).Data;
try
{
Assert.IsTrue(queryResult.ReturnData != null);

}
catch (Exception ex)
{
Assert.Fail(ex.Message);
}

And don't serialize the data by urself. RestSharp will take care of serializing.

How to post complex Json data (body) using restsharp?

You can create an json object and assign your value to it, then you can serialize the json and send in the body

public class LoginInfo
{
public string loginInfoId { get; set; }
public DateTime loginDate { get; set; }
}

public class Context
{
public string contextInfoId { get; set; }
public string userId { get; set; }
public string specificOs { get; set; }
public string buildPlatform { get; set; }
public string deviceName { get; set; }
public string deviceId { get; set; }
public string token { get; set; }
public LoginInfo loginInfo { get; set; }
}

public IRestResponse Post_New_RequestType(string context, string user_ID, string Specific_Os, string Build_Platfrom, string Device_Name, string device_Id, string Token_Value, string login_infoId, DateTime Login_Date)
{

Context tmp = new Context();
tmp.contextInfoId = context;
tmp.userId = user_ID;
tmp.specificOs = Specific_Os;
tmp.buildPlatform = Build_Platfrom;
tmp.deviceName = Device_Name;
tmp.deviceId = device_Id;
tmp.token = Token_Value;
tmp.loginInfo.loginInfoId = login_infoId;
tmp.loginInfo.loginDate = Login_Date;

string json = JsonConvert.SerializeObject(tmp);
var Client = new RestClient(HostUrl);
var request = new RestRequest(Method.POST);
request.Resource = string.Format("/api/example");
request.AddParameter("application/json", json, ParameterType.RequestBody);
IRestResponse response = Client.Execute(request);
return response;
}

RestSharp AddBody adding double quote in JSON parameter


You can do this using the request.AddParameter() method:

request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", data , ParameterType.RequestBody);

var response = client.Execute(request);
var content = response.Content; // raw content as string

Where data is of the format:

data :

{
"action":"dosomething" ,
"data":"somedata" ,
"token":"sometoken",
"jsonAction": {
"tokenId": "",
...
}

Hope it helps!



Related Topics



Leave a reply



Submit