0

I am trying to use REST, to send to an exposed webservice. But what I get is 400 (Bad Request.)

How do you property send a model across from REST, that will correctly map up to the model on the other side. What I don't want to do, is have all 100 parameters exposed individually in the connection point.

    [OperationContract]
    [WebInvoke(
        Method = "POST",
        UriTemplate = "SaveData",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
    void SaveData(datum data);

But what I am unable to do is pass a valid datum object.

     // Build JSON string
     JSONStringer vehicle;
    try {
        vehicle = new JSONStringer()
             .object()
                 .key("datum")
                     .object()
                         .key("ID").value(5)
                         .key("Name").value("test")
                         .key("No").value(54)
                         .key("Description").value("Test")
                     .endObject()
                 .endObject(); 

      HttpPost request = new HttpPost(address + "/SaveData");
     Log.d("WebInvoke", "Connection : " + address + "/SaveData");
     request.setHeader("Accept", "application/json");
     request.setHeader("Content-type", "application/json");
         StringEntity entity = new StringEntity(vehicle.toString());
         Log.d("StringEntity", vehicle.toString());
         request.setEntity(entity);

         // Send request to WCF service
         DefaultHttpClient httpClient = new DefaultHttpClient();
         HttpResponse response = httpClient.execute(request);

         Log.d("WebInvoke", "Saving : " +    response.getStatusLine().getStatusCode());
             // Saving : 400

Datum Class:

[DataContract]
public partial class datum
{
    [DataMember(Name = "ID")]
    public int ID { get; set; }
       [DataMember(Name = "Name")]
    public string Name { get; set; }
     [DataMember(Name = "No")]
    public Nullable<int> No { get; set; }

        [DataMember(Name = "Description")]
    public string Description { get; set; }
}
IAmGroot
  • 13,760
  • 18
  • 84
  • 154

1 Answers1

1

It should be:

vehicle = new JSONStringer()
             .object()
                 .key("data")
                     .object()
                         .key("ID").value(5)
                         .key("Name").value("test")
                         .key("No").value(54)
                         .key("Description").value("Test")
                     .endObject()
                 .endObject(); 

Source: WCF BodyStyle WrappedRequest doesn't work for incoming JSON param?

Community
  • 1
  • 1
Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93
  • Thanks, I now have `{"data":{"ID":5,"Name":"test","No":54,"Description":"Test"}}` but still get the 400 error :( – IAmGroot Jul 31 '13 at 09:27
  • added to question. and +1 for all help – IAmGroot Jul 31 '13 at 09:32
  • Do you get any response body from the server? – Oskar Kjellin Jul 31 '13 at 09:33
  • Just bad request: 400. I cant run it from browser, as method is not allowed (Post method), and Ive no idea how to build it in fiddler2, nor does it intercept the call from android. So am unsure on the body. I changed it to return an int. I can try and get that on android. – IAmGroot Jul 31 '13 at 09:39
  • Hey, I have actually just put `return data.ID + data.Name + data.Description + data.No;` at the top of the method, and changed return type to string. And the android recieves `"5testTest54"` So it is something in my method. Of which I can investigate myself. You've answered my question of mapping models, to WCF. Thanks! – IAmGroot Jul 31 '13 at 09:50