Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I call web service WCF by php curl

iservice.cs

{
    [OperationContract]
    [WebInvoke(UriTemplate = "/SaveData",Method = "*",BodyStyle =  WebMessageBodyStyle.ق  , ResponseFormat = WebMessageFormat.Json,RequestFormat =WebMessageFormat.Json)]
string SaveData(String DataResult);
}


service.cs
public class Service : IService
{
   public string SaveData(String DataResult)
    {

        return "OK";
    }
}


when i post json by this code
CURLOPT_POSTFIELDS => "\r\n\"{results:[{msgId:'1005',to:'966001',status:'D'},{msgId:'1005',to:'966002',status:'D'}]}\"",


return "OK"

But when i post json by this code
CURLOPT_POSTFIELDS => "{results:[{msgId:'1005',to:'966001',status:'D'},{msgId:'1005',to:'966002',status:'D'}]}",


return error :
The server encountered an error processing the request. The exception message is 'There was an error checking start element of object of type JsonData. The token '"' was expected but found 'r'.'. See server logs for more details. The exception stack trace is:


Difference between them "\r\n\"

The problem is String variable not accepting json encoding.

I want to solve in Iservice.cs because the json is post by a company and can not be changed it

How to modify the String DataResult to receive the posting json

What I have tried:

I change the string with this

string SaveData(RootObject DataResult);
}

[DataContract]
public class JsonData
{
    [DataMember]
    public string msgId { get; set; }
    [DataMember]
    public string to { get; set; }
    [DataMember]
    public string status { get; set; }
}

[DataContract]
public class RootObject
{
    [DataMember]
    public List<JsonData> results { get; set; }
}
Posted
Comments
Richard MacCutchan 6-Mar-18 4:07am    
You need to include the escaped double quotes at the beginning and end of your JSON string.
Member 12857222 6-Mar-18 4:40am    
CURLOPT_POSTFIELDS => "{results:[{msgId:'1005',to:'966001',status:'D'},{msgId:'1005',to:'966002',status:'D'}]}",

A JSON string sent from the company and can not be modified

I want to edit the code iservice.cs
Richard MacCutchan 6-Mar-18 4:43am    
So how do you think anyone here can help?
Member 12857222 6-Mar-18 5:21am    
When using a Postman running on a Mac or Linux system the JSON sender will be in this format : "{results:[{msgId:'1005',to:'966001',status:'D'},{msgId:'1005',to:'966002',status:'D'}]}"
Richard MacCutchan 6-Mar-18 5:30am    
Then you need to find out why they are sending in the wrong format.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900