Click here to Skip to main content
15,566,160 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

WCF Test Client is getting response as escape sequence Json Format in IOS and Android.

"{\"BusID\":\"1\",\"Latitude\":\"23\",\"Longitude\":\"34\",\"TrackTime\":\"\"}"


But It is showing error for below format

{"BusID":"1","Latitude":"23","Longitude":"34","TrackTime":""}..

Showing below error


CSS
Status
400 Bad Request Show explanation Loading time: 15
Request headers
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Response headers
Cache-Control: private
Content-Length: 1780
Content-Type: text/html
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 23 Apr 2014 04:09:47 GMT


my WCF Sample code

VB
[OperationContract]
      [WebInvoke(UriTemplate = "UpdateTrackDetails",
          Method = "POST")]
      int UpdateTrackDetails(string trackdetails);


//
C#
public int UpdateTrackDetails(string trackdetails)
       {
           int result = 0;
           TrackDetails _BusTrack = JsonConvert.DeserializeObject<TrackDetails>(trackdetails);

           using (var context = new SchoolBusTrackEntities())
           {
               context.BusTrackDetails.Add(
                   new BusTrackDetail
                   {

                       BusID = Convert.ToInt32(_BusTrack.BusID),
                       Latitude = _BusTrack.Latitude,
                       Longitude = _BusTrack.Longitude,
                       TrackTime =DateTime.Now,
                   }
                   );

result = context.SaveChanges();
             
           }
           return result;

       }



I am using here HttpClient. Issue I am mainly getting for WCF service consuming by IOS and Android applications.

My sample wcf test client

static async Task Post(string BusID, string Latitude, string Longitude, string TrackTime)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("http://192.168.1.42/BusTrackService/");
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
BusTrack1 _Bus = new BusTrack1();
_Bus.BusID = BusID;
_Bus.Latitude = Latitude;
_Bus.Longitude = Longitude;
_Bus.TrackTime = TrackTime;
string data = JsonConvert.SerializeObject(_Bus);//{"busid:12"}
HttpResponseMessage response = client.PostAsJsonAsync("SchoolBusTrack.svc/UpdateTrackDetails", data).Result;
if (response.IsSuccessStatusCode)
{
var product = await response.Content.ReadAsAsync<string>();
}

}
}


Thanks in advance...
Posted

1 solution

Try setting the content type to "application/json" on your clients when sending.
 
Share this answer
 
Comments
Santhosh23 23-Apr-14 1:30am    
already is there
Mehdi Gholam 23-Apr-14 1:54am    
Your error says "text/html"

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