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

后端 未结 4 1211
抹茶落季
抹茶落季 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:11

    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 from the command line.

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

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

    $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'
    

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

    0 讨论(0)
  • 2020-12-08 10:12

    Well Curl could be a better option for json representation but in that case it would be difficult to understand the structure of json because its in command line. if you want to get your json on browser you simply remove all the XML Annotations like -

    @XmlRootElement(name="person")
    @XmlAccessorType(XmlAccessType.NONE)
    @XmlAttribute
    @XmlElement
    

    from your model class and than run the same url, you have used for xml representation.

    Make sure that you have jacson-databind dependency in your pom.xml

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.4.1</version>
    </dependency>
    
    0 讨论(0)
  • 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"
    }
    
    0 讨论(0)
  • 2020-12-08 10:27

    Basically I use Fiddler or Postman for testing API's.

    In fiddler, in request header you need to specify instead of xml, html you need to change it to json. Eg: Accept: application/json. That should do the job.

    0 讨论(0)
提交回复
热议问题