12

I have a a view model that looks like.

public class StoreItemViewModel
{
    public Guid ItemId { get; set; }
    public List<Guid> StoreIds { get; set; }
    [Required]
    public string Description { get; set; }
    //[Required]
    //[DataMember(IsRequired = true)]
    public int ItemTypeId { get; set; }


}

I have a small helper that using is using RestSharp.

public static IRestResponse Create<T>(object objectToUpdate, string apiEndPoint) where T : new()
    {
        var client = new RestClient(CreateBaseUrl(null))
        {
            Authenticator = new HttpBasicAuthenticator("user", "Password1")
        };

        var request = new RestRequest(apiEndPoint, Method.POST);
        //request.JsonSerializer = new JsonSerializer();
       // {RequestFormat = DataFormat.Json};
        request.AddObject(objectToUpdate);
       // clientJsonSerializer = new YourCustomSerializer();
        var response = client.Execute<T>(request);
        return response;
    }

When debugging the controller within my api

 [HttpPost]
    public HttpResponseMessage Create([FromBody]StoreItemViewModel myProduct)
    {
        //check fields are valid
     .........
     }

myProducts products are all populated apart from the public List StoreIds it always is returning a single reward with an empty Guid. Even if I have added 2 or more StoreIds

I assume this is because I am doing something wrong with my Create helper within my application.

Can anyone help with this its causing a major headache.

The raw data sent to the webapi is looking like

ItemId=f6dbd244-e840-47e1-9d09-53cc64cd87e6&ItemTypeId=6&Description=blabla&StoreIds=d0f36ef4-28be-4d16-a2e8-37030004174a&StoreIds=f6dbd244-e840-47e1-9d09-53cc64cd87e6&StoreId=d0f36ef4-28be-4d16-a2e8-37030004174a
Ev.
  • 7,109
  • 14
  • 53
  • 87
Diver Dan
  • 9,953
  • 22
  • 95
  • 166
  • could you share how the raw json request looks like? – Kiran Sep 04 '12 at 19:04
  • @KiranChalla sorry how do I get that using the restsharp bits? – Diver Dan Sep 04 '12 at 19:21
  • you could use Fiddler, which is a httpproxy, to capture the requests/responses. – Kiran Sep 04 '12 at 20:33
  • haha I didnt know I could use fiddler for the serverside stuff. I always thought it was just for clientside stuff. Thanks for the tip :) I just updated the post with the raw json – Diver Dan Sep 04 '12 at 20:45
  • So I have gotten as far as seeing that the StoreIds is not set properly when the data is serialized. I tried testing with the json.net library and it works fine. If I need to use the json.net library I am happy to do that I just dont know how to pass the json.net serlialized object to the api – Diver Dan Sep 05 '12 at 12:08

3 Answers3

23

RestSharp now has a more streamlined way to add an object to the RestRequest Body with Json Serialization:

public static IRestResponse Create<T>(object objectToUpdate, string apiEndPoint) where T : new()
{
    var client = new RestClient(CreateBaseUrl(null))
    {
        Authenticator = new HttpBasicAuthenticator("user", "Password1")
    };
    var request = new RestRequest(apiEndPoint, Method.POST);
    request.AddJsonBody(objectToUpdate); // HERE
    var response = client.Execute<T>(request);
    return response;
}

This was found in RestSharp 105.0.1.0

Morphed
  • 3,527
  • 2
  • 29
  • 55
20

I managed to get this working. I don't think its the correct way but it works.

 public static IRestResponse Create<T>(object objectToUpdate, string apiEndPoint) where T : new()
    {
        var client = new RestClient(CreateBaseUrl(null))
        {
            Authenticator = new HttpBasicAuthenticator("user", "Password1")
        };
        var json = JsonConvert.SerializeObject(objectToUpdate);
        var request = new RestRequest(apiEndPoint, Method.POST);
        request.AddParameter("text/json", json, ParameterType.RequestBody);
        var response = client.Execute<T>(request);
        return response;
    }
Diver Dan
  • 9,953
  • 22
  • 95
  • 166
14

I struggled with the same problem and came up a working solution.

  1. Be sure to set the request format to JSON:

    request.RequestFormat = DataFormat.Json;

  2. Use AddBody, rather than AddObject:

    request.AddBody(zNewSessionUsage);

So your code would be something like this:

public static IRestResponse Create<T>(object objectToUpdate, string apiEndPoint) where T : new()
{
    var client = new RestClient(CreateBaseUrl(null))
    {
        Authenticator = new HttpBasicAuthenticator("user", "Password1")
    };

    var request = new RestRequest(apiEndPoint, Method.POST);
    request.RequestFormat = DataFormat.Json;
    request.AddBody(objectToUpdate);
    var response = client.Execute<T>(request);
    return response;
}
bigtech
  • 464
  • 3
  • 6
  • 1
    And it appears .RequestFormat must be listed before .AddBody as per the example above otherwise it'll still default to XML Serialization. – Jafin Jan 29 '15 at 05:43