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.
It's better to specify the Json data body. var client = new RestClient("URL"); var request = new RestRequest("Resources",Method. POST); request. RequestFormat = RestSharp.
AddHeader("Content-Type", "application/json; CHARSET=UTF-8"); request. AddJsonBody(jsonString); var response = client. Execute(request); Console. WriteLine(response.
var client = new RestClient("http://localhost"); var request = new RestRequest("resource", Method. POST); request. AddParameter("auth_token", "1234"); request. AddBody(json); var response = client.
Since RestSharp uses the HttpClient, we should consider similar issues regarding the RestClient instantiation.
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
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 !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With