REST API - Use the “Accept: application/json” HTTP Header

后端 未结 4 1224
抹茶落季
抹茶落季 2020-12-08 10:08

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: Use the Accept: application/json

4条回答
  •  死守一世寂寞
    2020-12-08 10:24

    Here's a handy site to test out your headers. 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:-

    $ 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"
    }
    

提交回复
热议问题