Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp Post a JSON Object

Tags:

json

c#

restsharp

I am trying to post the following JSON with RestSharp:

{"UserName":"UAT1206252627", "SecurityQuestion":{     "Id":"Q03",     "Answer":"Business",     "Hint":"The answer is Business" }, } 

I think that I am close, but I seem to be struggling with the SecurityQuestion (the API is throwing an error saying a parameter is missing, but it doesn't say which one)

This is the code I have so far:

var request = new RestRequest("api/register", Method.POST); request.RequestFormat = DataFormat.Json;  request.AddParameter("UserName", "UAT1206252627");  SecurityQuestion securityQuestion = new SecurityQuestion("Q03"); request.AddParameter("SecurityQuestion", request.JsonSerializer.Serialize(securityQuestion));  IRestResponse response = client.Execute(request); 

And my Security Question class looks like this:

public class SecurityQuestion {     public string id {get; set;}     public string answer {get; set;}     public string hint {get; set;}      public SecurityQuestion(string id)     {          this.id = id;          answer = "Business";          hint = "The answer is Business";     } } 

Can anyone tell me what I am doing wrong? Is there any other way to post the Security Question object ?

Many thanks.

like image 323
JamesWillett Avatar asked Aug 11 '15 09:08

JamesWillett


People also ask

How do I send a post request on RestSharp?

It's better to specify the Json data body. var client = new RestClient("URL"); var request = new RestRequest("Resources",Method. POST); request. RequestFormat = RestSharp.

How pass JSON body in rest Sharp?

AddHeader("Content-Type", "application/json; CHARSET=UTF-8"); request. AddJsonBody(jsonString); var response = client. Execute(request); Console. WriteLine(response.

How do you add parameters in RestSharp?

var client = new RestClient("http://localhost"); var request = new RestRequest("resource", Method. POST); request. AddParameter("auth_token", "1234"); request. AddBody(json); var response = client.

Does RestSharp use HttpClient?

Since RestSharp uses the HttpClient, we should consider similar issues regarding the RestClient instantiation.


2 Answers

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 
like image 96
Oluwafemi Avatar answered Oct 26 '22 09:10

Oluwafemi


Thanks again for your help. To get this working I had to submit everything as a single parameter. This is the code I used in the end.

First I made a couple of classes called Request Object and Security Question:

public class SecurityQuestion {     public string Id { get; set; }     public string Answer { get; set; }     public string Hint { get; set; } }  public class RequestObject {     public string UserName { get; set; }     public SecurityQuestion SecurityQuestion { get; set; } } 

Then I just added it as a single parameter, and serialized it to JSON before posting it, like so:

var yourobject = new RequestObject             {                 UserName = "UAT1206252627",                 SecurityQuestion = new SecurityQuestion                 {                     Id = "Q03",                     Answer = "Business",                     Hint = "The answer is Business"                 },             }; var json = request.JsonSerializer.Serialize(yourobject);  request.AddParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);  IRestResponse response = client.Execute(request); 

and it worked !

like image 44
JamesWillett Avatar answered Oct 26 '22 08:10

JamesWillett