Get JSON headers working

https://developer.atlassian.com/server/crowd/json-requests-and-responses/


https://stackoverflow.com/questions/46594629/php-headercontent-type-application-json-is-not-working


https://silex.symfony.com/doc/2.0/cookbook/json_request_body.html


https://webmasters.stackexchange.com/questions/31212/difference-between-the-accept-and-content-type-http-headers


https://stackoverflow.com/questions/43209924/rest-api-use-the-accept-application-json-http-header

When I make a request, I get a response in XML, but what I need is JSON. In the doc it is stated in order to get a JSON in return:

[code style=”css”]Use the Accept: application/json HTTP Header.[/code]

Where do I find the HTTP Header to put Accept: application/json inside?

My guess is it is not suppose to be inside the URL-request, which looks like:

http://localhost:8080/otp/routers/default/plan?fromPlace=52.5895,13.2836&toPlace=52.5461,13.3588&date=2017/04/04&time=12:00:00
json rest request-headers


You guessed right, HTTP Headers are not part of the URL.

And when you type a URL in the browser the request will be issued with standard headers. Anyway REST Apis are not meant to be consumed by typing the endpoint in the address bar of a browser.

The most common scenario is that your server consumes a third party REST Api.

To do so your server-side code forges a proper GET (/PUT/POST/DELETE) request pointing to a given endpoint (URL) setting (when needed, like your case) some headers and finally (maybe) sending some data (as typically occurrs in a POST request for example).

The code to forge the request, send it and finally get the response back depends on your server side language.

If you want to test a REST Api you may use curl tool – https://curl.haxx.se/ – from the command line.

curl makes a request and output the response to stdout (unless otherwise instructed).

In your case the test request would be issued like this:

[code style=”php”]
$curl -H "Accept: application/json" ‘http://localhost:8080/otp/routers/default/plan?fromPlace=52.5895,13.2836&toPlace=52.5461,13.3588&date=2017/04/04&time=12:00:00’
[/code]

The H or –header directive sets a header and its value.

Another way to test between different Request header values is to download and use the Postman client. It takes a little setup but is made to test REST API’s

Here’s a handy site to test out your headers http://gethttp.info/ .

You can see your browser headers and also use cURL to reflect back whatever headers you send.

For example, you can validate the content negotiation like this.

This Accept header prefers plain text so returns in that format:-

[code style=”php”]
$ curl -H "Accept: application/json;q=0.9,text/plain" http://gethttp.info/Accept
application/json;q=0.9,text/plain
Whereas this one prefers JSON and so returns in that format:-

$ curl -H "Accept: application/json,text/*;q=0.99" http://gethttp.info/Accept
{
"Accept": "application/json,text/*;q=0.99"
}
[/code]


[wpdreams_rpp id=0]

Scroll to Top