1

Followup on my question Getting null values when deserializing a list using RestSharp

I have another problem now. I need to call POST and the json body should look like this:

{"email": {"evstatus": "processed"}}

My code look like this

class Email
{
    public string Evexpire { get; set; }
    public string Evfields { get; set; }
    public string Evsysseq { get; set; }
    public string Evtime { get; set; }
    public string Evtype { get; set; }
    public string Evstatus { get; set; }
}

var client = new RestClient("xxx");
client.Authenticator = new HttpBasicAuthenticator("xx", "x");
var request = new RestRequest("xxxx/action/processed", Method.POST);
request.RequestFormat = DataFormat.Json;
request.RootElement = "email";
request.AddJsonBody(new Email  { Evstatus = "processed" }  );

But I receive this error:

"StatusCode: BadRequest, Content-Type: application/json;charset=utf-8, Content-Length: 0)"

When I look at the request in the debugger I see this in the parameter list which apart from the fields with null values do not look like what I need.

{application/json={"Evexpire":null,"Evfields":null,"Evsysseq":null,"Evtime":null,"Evtype":null,"Evstatus":"processed"}}

What should I change/add to get this to work? (I got this request to work in SoapUI)

Community
  • 1
  • 1
Tommy
  • 161
  • 1
  • 2
  • 15

2 Answers2

1

Most probably you'll have to add the Content-Type-header to the request, too:

request.AddHeader("Content-Type", "application/json; charset=utf-8");

Have a look here also. Hope that helps too.

Community
  • 1
  • 1
StackUseR
  • 884
  • 1
  • 11
  • 40
1

I solved it myself. Probably not the prettiest solution but changing my code like this worked:

    Email jsonElement = new Email();
    jsonElement.Evstatus = "processed";
    EmailObj jsonBody = new EmailObj();
    jsonBody.email = jsonElement;
    request.AddJsonBody(jsonBody);
Tommy
  • 161
  • 1
  • 2
  • 15