简体   繁体   中英

WCF RESTful service not accepting JSON input

In my case, i have a web service as below,

    [OperationContract]
    [WebInvoke(UriTemplate = "services/CreatePerson", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST")]
    string CreatePerson(string data);

Its expecting a JSON input. while i am testing this service with Fiddler by passing a JSON string as Request body as below,

"{"personName":"Joe", "source":"I", "address":"KK Road"}"

and Request Header as

User-Agent: Fiddler
Content-Type: application/json;charset=utf-8
Host: localhost
Content-Length: 54

Its not hitting the service method break point while debugging.

At the same time its working for the following JSON as below(replacing double quotes with single quote the previous json),

"{'personName':'102',  'source':'I',  'address':'KK Road'}"

The service method not taking the JSON string input, as it works well if i pass input as "test"..

Where is the actual problem, please help me to find out....

I don't think you should use the initial and trailing quotes!

Try:

{'personName':'102',  'source':'I',  'address':'KK Road'}

Also you method should not take a string argument but a class that conforms to the json.

 public class M
 {
     public string personName { get; set; }
     public string source { get; set; }
     public string address { get; set; }
 }

 [OperationContract]
 [WebInvoke(UriTemplate = "services/CreatePerson", RequestFormat = WebMessageFormat.Json, 
    ResponseFormat = WebMessageFormat.Json, Method = "POST")]
 string CreatePerson(M data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM