OhMyGuruFR
OhMyGuruFR

Reputation: 65

RestSharp POST Object as JSON

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.

Upvotes: 7

Views: 18073

Answers (3)

Evk
Evk

Reputation: 101583

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.

Upvotes: 1

phuzi
phuzi

Reputation: 13079

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;
}

Upvotes: 2

Palle Due
Palle Due

Reputation: 6312

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

Upvotes: 1

Related Questions