REST API - 使用“接受:应用程序/json”HTTP 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43209924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
REST API - Use the "Accept: application/json" HTTP Header
提问by obrob
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/jsonHTTP Header.
当我发出请求时,我会收到 XML 格式的响应,但我需要的是 JSON。在文档中,为了获得 JSON 的回报而声明:使用Accept: application/jsonHTTP 标头。
Where do I find the HTTP Header to put Accept: application/jsoninside?
我在哪里可以找到要放入其中的 HTTP 标头Accept: application/json?
My guess is it is not suppose to be inside the URL-request, which looks like:
我的猜测是它不应该在 URL 请求中,它看起来像:
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
回答by Paolo
You guessed right, HTTP Headers are not part of the URL.
您猜对了,HTTP 标头不是 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.
当您在浏览器中输入 URL 时,该请求将使用标准标头发出。无论如何,REST API 并不意味着通过在浏览器的地址栏中键入端点来使用。
The most common scenario is that your server consumes a third party REST Api.
最常见的情况是您的服务器使用第三方 REST Api。
To do so your server-side code forgesa proper GET (/PUT/POST/DELETE) request pointing to a given endpoint (URL) setting (when needed, like your case) some headersand finally (maybe) sending some data (as typically occurrs in a POST request for example).
为此,您的服务器端代码会伪造一个正确的 GET (/PUT/POST/DELETE) 请求,该请求指向给定的端点 (URL) 设置(在需要时,如您的情况)一些标头,最后(可能)发送一些数据(如例如,通常发生在 POST 请求中)。
The code to forge the request, send it and finally get the response back depends on your server side language.
伪造请求、发送请求并最终返回响应的代码取决于您的服务器端语言。
If you want to testa REST Api you may use curltool from the command line.
如果您想测试REST Api,您可以使用curl命令行中的工具。
curlmakes a request and output the response to stdout (unless otherwise instructed).
curl发出请求并将响应输出到标准输出(除非另有说明)。
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 Hor --headerdirective sets a header and its value.
的H或--header指令集的头和它的值。
回答by Echelon
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.
这是一个方便的站点来测试您的标题。您可以查看浏览器标头,还可以使用 cURL 来反映您发送的任何标头。
For example, you can validate the content negotiation like this.
例如,您可以像这样验证内容协商。
This Acceptheader prefers plain text so returns in that format:-
此Accept标题更喜欢纯文本,因此以该格式返回:-
$ 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:-
而这个人更喜欢 JSON,因此以该格式返回:-
$ curl -H "Accept: application/json,text/*;q=0.99" http://gethttp.info/Accept
{
"Accept": "application/json,text/*;q=0.99"
}
回答by Preet Singh
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 Annotationslike -
好吧,Curl 可能是 json 表示的更好选择,但在这种情况下,很难理解 json 的结构,因为它在命令行中。如果您想在浏览器上获取 json,只需删除所有XML 注释,例如 -
@XmlRootElement(name="person")
@XmlAccessorType(XmlAccessType.NONE)
@XmlAttribute
@XmlElement
from your model class and than run the same url, you have used for xml representation.
从您的模型类,而不是运行相同的 url,您已用于 xml 表示。
Make sure that you have jacson-databind dependency in your pom.xml
确保在 pom.xml 中有 jacson-databind 依赖项
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.4.1</version>
</dependency>

