× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

If your RESTful server API expects a JSON request, you need a client that can send JSON.

Vasudev Ram
Sep 28, 2014
<p>A mistake sometimes made by beginners to RESTful JSON web apps:</p><p>Lets say you write a RESTFUL server API/app that expects an HTTP request in <span style="color:rgb(95, 99, 102); font-family:lato,sans-serif; font-size:18.3999996185303px">JSON format (i.e. </span>Content-Type = 'application/json') from the client, and the server validates that the Content-Type is actually 'application/json', before processing the request. The request could be of the form "http://example.com/API/vi/login?user_id=your_name".</p><p>Then, in order to test the server, you need a client that can send a JSON HTTP request (and specifically, not an HTTP request where the Content-Type is HTML). So just pasting the URL of the API into a browser tab will likely not work, because by default the browser will not send the request with a HTTP header of Content-Type = 'application/json'. It may send it with no Content-Type header or a Content-Type header = 'text/html', which is the common default for browsers. So you either need to write some JavaScript code in an HTML page, which sends the right Content-Type along with the request, or need to write a command-line (or GUI) client that does the same. The same kind of logic also holds for handling the response from the server: If the response is also in JSON format, then your client should be able to parse JSON and do something meaningful with it, such as showing it to the user, or doing some computation on the JSON response data, and then showing the results of that computation to the user. So by default, if you use a browser as the client, the JSON data may be formatted and displayed by the browser to you (because some browsers support doing that), but in order to do any computation on that JSON data, you will need some JavaScript code running in the page that receives the response.</p><p> </p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus