7

Here is my class:

public class PTList
{
    private String name;

    public PTList() { }
    public PTList(String name)
    {
        this.name = name;
    }


    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

}

and my RestSharp POST Request:

    protected static IRestResponse httpPost(String Uri, Object Data)
    {
        var client = new RestClient(baseURL);
        client.AddDefaultHeader("X-Authentication", AuthenticationManager.getAuthentication());
        client.AddDefaultHeader("Content-type", "application/json");
        var request = new RestRequest(Uri, Method.POST);

        request.RequestFormat = DataFormat.Json;

        request.AddJsonBody(Data);

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

and when I use the httpPost method with the good URI and a PTList object, the front API anwser that "name" is null. I think that my PTList object is not serialized as a valid JSON in the request for the API, but can't understand what's going wrong.

OhMyGuruFR
  • 65
  • 1
  • 1
  • 4
  • 1
    A few things you can try, first make sure the `Data` you pass is in valid format by adding a breakpoint, or showing it on Console, secondly you could try a simpler class such as `public class PTList { public string name { get; set; } }` with public accessible field name. – Keyur PATEL Jun 20 '17 at 07:47
  • AFAIK serialization will serialize the **properties** and no properties => no content, just an empty object like {} – Sir Rufo Jun 20 '17 at 07:47

3 Answers3

2

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;
}
phuzi
  • 12,078
  • 3
  • 26
  • 50
1

Json serializer used by RestSharp by default does not serialize private fields. So you can change your class like this:

public class PTList
{        
    public PTList() { }

    public PTList(String name) {
        this.name = name;
    }

    public string name { get; set; }
}

And it will work fine.

If capabilities of default serializer will be not enough (as far as I know - you cannot even rename properties with it, to make Name serialize as name for example) - you can use better serializer, like JSON.NET, like described here for example.

Evk
  • 98,527
  • 8
  • 141
  • 191
1

You could try this instead of AddJsonBody:

request.AddParameter("application/json; charset=utf-8", JsonConvert.SerializeObject(Data), ParameterType.RequestBody);

It's one of the solutions here: How to add json to RestSharp POST request

Palle Due
  • 5,929
  • 4
  • 17
  • 32