3

So I have the following service:

public interface IService1
{
    [OperationContract, WebGet(UriTemplate = "/getStuff?stuff={stuff}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    List<Row> getdTable(string stuff);
    [OperationContract, WebInvoke(UriTemplate = "/log", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST")]
    void insertLog(Log[] log);
    [OperationContract, WebInvoke(UriTemplate = "/testPost", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST")]
    void testPost(TwoStrings testPost);
}

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]


[DataContract]
public class TwoStrings
{
    [DataMember]
    public string one { get; set; }
    [DataMember]
    public string two { get; set; }
}

The WebGet works like a charm, but I can't get any of the webinvoke methods to actually work. I'm testing it using fiddler right now, using the following POST:

POST http://localhost:50051/Service1.svc/testPost HTTP/1.1  
Host: localhost:50051  
User-Agent: Fiddler   
Content-Type: application/json; charset=utf-8  
Content-Length: 25  
{"testPost":{"one":"test1","two":"test2"}}

And the response I get back is:

 HTTP/1.1 400 Bad Request

I'm at a loss here, I've tried several different formats on the JSON body to no avail. Is the JSON properly formatted? Is there any configuration required to do a POST?

edit: Solution found, I'm an IDIOT. The last time I corrected the fiddler request to reflect the proper body wrapping I forgot to alter the body size from 25 to the correct 38. Thanks for the quick responses!

Patric Cunha
  • 97
  • 1
  • 5
  • try specifying the `datatype` like this: `dataType: "json"` – Saravanan May 30 '13 at 14:32
  • @CodeCaster I was under the impression the wrapped object was done based on the parameter name. How do I enable that btw? – Patric Cunha May 30 '13 at 14:40
  • @saravanan Where do I specify the dataType? – Patric Cunha May 30 '13 at 14:41
  • You can specify it in the ajax request like this ` $.ajax({ url: 'http://localhost:50051/Service1.svc/testPost', type: "POST", contentType: "application/json; charset=utf-8", data: {"testPost":{"one":"test1","two":"test2"}}, dataType: "json", success: function (data) { response.val(data.d); }, error: function (request, status, error) { //TODO: do something here } }); ` – Saravanan May 31 '13 at 05:31
  • I wasn't posting it using ajax yet, but that's cool to know. Should have mentioned I was using fiddler to compose the request. – Patric Cunha May 31 '13 at 08:13
  • What was the "proper body wrapping"? – Andrew Gee Jul 21 '14 at 10:54

0 Answers0