REST API-使用“接受:应用程序/json" HTTP头 [英] REST API - Use the "Accept: application/json" HTTP Header

查看:102
本文介绍了REST API-使用“接受:应用程序/json" HTTP头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发出请求时,我得到XML响应,但我需要的是JSON.在文档中进行了说明,以获取JSON作为回报:使用Accept: application/json HTTP标头.

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 HTTP Header.

在哪里可以找到将Accept: application/json放入其中的HTTP标头?

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

我的猜测是它不是应该位于URL请求中,它看起来像:

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

推荐答案

您猜对了,HTTP标头不是URL的一部分.

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

当您在浏览器中键入URL时,将使用标准标头发出请求.无论如何,不​​要通过在浏览器的地址栏中键入终结点来使用REST Apis.

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.

最常见的情况是您的服务器使用了第三方REST Api.

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

为此,您的服务器端代码伪造正确的GET(/PUT/POST/DELETE)请求,该请求指向给定的端点(URL)设置(在需要时,如您的情况一样),某些 headers ,最后(也许)发送一些数据(例如,通常在POST请求中出现).

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.

如果您要测试 REST Api,则可以使用 curl 工具.

If you want to test a REST Api you may use curl tool from the command line.

curl发出请求,并将响应输出到stdout(除非另有指示).

curl makes a request and output 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'

H--header指令设置标头及其值.

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

这篇关于REST API-使用“接受:应用程序/json" HTTP头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆