Sagar Jagadesh
Sagar Jagadesh

Reputation: 247

Post json string to WCF method

I have WCF method

[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
string PostNewOrder(string OrderData);

This is the json string which I am posting

{
    "customerId": " ",
    "langCode": "SE",
    "timeZone": "38",
    "orderNumber": "1122519",
    "orderDate": "2016-04-13 15:56:36",
    "deliveryNumber": "625615",
    "devices": "000000001050840;",
    "transactionId": "24",
    "shipDate": "2016-04-13 16:41:31"
}

But I am getting OrderData as null in the WCF method

If I post string

"{\"customerId\":\" \",\"langCode\":\"SE\",\"timeZone\":\"38\",\"orderNumber\":\"1122519\",\"orderDate\":\"2016-04-13 15:56:36\",\"deliveryNumber\":\"625615\",\"devices\":\"000000001050840;\",\"transactionId\":\"24\",\"shipDate\":\"2016-04-13 16:41:31\"}"

It works fine but this is not a proper json , Thanks in Advance..

Upvotes: 0

Views: 757

Answers (3)

chaya D
chaya D

Reputation: 161

this is an old question, but in case anyone needs an answer for this:

you will need to wrap your json string in anoter string.

i was using angular - so in my case i did this:

let doc = new FileData();
...
let jsonObj = JSON.stringify(doc); //json object string

let strObj = JSON.stringify(jsonObj); //json object string wrapped with string

Upvotes: 0

Amit Kumar Ghosh
Amit Kumar Ghosh

Reputation: 3726

your contract should look something like -

    [OperationContract]
    [WebInvoke(Method = "POST",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json ,
    UriTemplate="/post")]
    string PostNewOrder(RootObject OrderData);

where RootObject should look like -

public class RootObject
{
    public string customerId { get; set; }
    public string langCode { get; set; }
    public string timeZone { get; set; }
    public string orderNumber { get; set; }
    public string orderDate { get; set; }
    public string deliveryNumber { get; set; }
    public string devices { get; set; }
    public string transactionId { get; set; }
    public string shipDate { get; set; }
}

what you are posting is a json object representation and not a string and the WCF Runtime is expected to deserialize the content to it's equivalent strongly typed object at server.

Upvotes: 1

Amr Deif
Amr Deif

Reputation: 103

I think you need to remove RequestFormat and ResponseFormat from OperationContract

Upvotes: 0

Related Questions