Saturday, May 22, 2010

Why don't I see my JSON RESTful response in the browser?

When testing RESTful services, one common option is to use JSON (Javascript Object Notation) which can significantly decrease the size of the service response. As we
A quick note on unexpected behavior (at first) when request the SAME url through different means.
If I request to get customer 1:
http://localhost/CustomerSearch/1
GET /CustomerSearch/1 HTTP/1.1


Host: localhost.:1046
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9 (.NET CLR 3.5.30729)
Accept: application/json, text/javascript, */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost:2592/
Origin: http://localhost:2592


My response is indeed JSON


HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Sat, 22 May 2010 06:12:06 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 153
Cache-Control: private
Content-Type: application/json; charset=utf-8
Connection: Close

{"DateOfHire":"\/Date(1072933200000-0500)\/","EmployeeId":"dd98655a-8706-4b2f-b648-874079015295","FirstName":"Jane","LastName":"Doe","SSN":"123-45-6789"}



If my request url stays the same, but I change my Accept: header, I no longer get JSON back but instead xml:


GET /CustomerSearch/1 HTTP/1.1
Host: localhost.:1046
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: ASP.NET_SessionId=yypfhriihodesh553qpmaam4



HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Sat, 22 May 2010 06:12:06 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 153
Cache-Control: private
Content-Type: application/json; charset=utf-8
Connection: Close

My response is indeed JSON
{"DateOfHire":"\/Date(1072933200000-0500)\/","EmployeeId":"dd98655a-8706-4b2f-b648-874079015295","FirstName":"Jane","LastName":"Doe","SSN":"123-45-6789"}

So what is going on here?
In one case Im request via the browser and I get xml back. In the other case I request via jQuery:
$.getJSON

which happily sends over

Accept: application/json, text/javascript, */*

2 comments:

Note: Only a member of this blog may post a comment.