简体   繁体   English

API 如何接收请求的方法、正文和标头?

[英]How do APIs receive the request's method, body, and headers?

The query URL / endpoint below seems to successfully make a request to the API resource and returns a JSON response:下面的查询 URL / 端点似乎成功地向 API 资源发出请求并返回 JSON 响应:

https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia/all-access/all-agents/Albert_Einstein/daily/2015100100/2015103100 https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia/all-access/all-agents/Albert_Einstein/daily/2015100100/2015103100

Given that a request is made up of not only the URL, but also of a method, headers, and body, could someone explain how the API knows which method to use, as well as how the headers and body get transmitted (if they did exist)?鉴于请求不仅由 URL 组成,还由方法、标头和正文组成,有人可以解释 API 如何知道使用哪种方法,以及标头和正文如何传输(如果他们这样做了存在)?

Given that a request is made up of not only the URL, but also of a method, headers, and body, could someone explain how the API knows which method to use, as well as how the headers and body get transmitted (if they did exist)?鉴于请求不仅由 URL 组成,还由方法、标头和正文组成,有人可以解释 API 如何知道使用哪种方法,以及标头和正文如何传输(如果他们这样做了存在)?

If I'm understanding your question correctly... the API doesn't know these things, the client knows these things.如果我正确理解您的问题...... API 不知道这些事情,客户知道这些事情。

Which is to say when I'm looking at your question in my web browser, what I'm really looking at is the web browsers interpretation of an HTML document.也就是说,当我在我的 web 浏览器中查看您的问题时,我真正看到的是 web 浏览器对 HTML 文档的解释。 Because the web browser speaks HTML, it knows what <a href="..."> means;因为 web 浏览器说 HTML,它知道<a href="...">是什么意思; that the quoted text is an identifier for another resource that I the use might want to navigate to.引用的文本是我可能想要导航到的另一个资源的标识符。

The browser also knows RFC 3986, so it knows how to parse the quoted string and extract from it the protocol, the host, and the target uri.浏览器知道 RFC 3986,因此它知道如何解析引用的字符串并从中提取协议、主机和目标 uri。

Because the browser also knows about https, it knows which port number it should default to when the port isn't specified.因为浏览器也知道 https,所以它知道在未指定端口时应该默认使用哪个端口号。

Because the browser knows HTTP, it knows how to construct a valid HTTP request, and the semantics of the required and optional headers it might want to attach.因为浏览器知道 HTTP,它知道如何构造一个有效的 HTTP 请求,以及它可能想要附加的必需和可选标头的语义。

Because HTTP follows the REST architectural style, we also know that the interface is uniform -- all HTTP resources use the same semantics.因为 HTTP 遵循 REST 架构风格,我们也知道接口是统一的——所有 HTTP 资源使用相同的语义。 So the browser doesn't need to know what the identifier is in order to know that GET , HEAD , OPTIONS are all safe.所以浏览器不需要知道标识符什么就可以知道GETHEADOPTIONS都是安全的。 Similarly, the rules for authentication, caching, content-negotiation, and so on are all the same, so the browser can craft the appropriate headers as it generates the request.同样,身份验证、缓存、内容协商等规则都是相同的,因此浏览器可以在生成请求时制作适当的标头。

For instance, the browser knows that it is itself HTML capable, so it includes headers that communicate a preference for an HTML or XHTML+xml representation of the resource, if one is available.例如,浏览器知道它本身支持 HTML,因此它包含的标头传达了对资源的 HTML 或 XHTML+xml 表示(如果可用)的偏好。

Were I to instead switch to the command line, I could use curl(1) to generate the http request instead, which would produce an HTTP request with different headers.如果我改为切换到命令行,我可以使用curl(1)来生成 http 请求,这将生成具有不同标头的 HTTP 请求。

The browser (and curl) know not to send a body with a HEAD or GET request because the HTTP specification explains that the payload of a GET or HEAD request has no defined semantics.浏览器(和 curl)知道不发送带有 HEAD 或 GET 请求的正文,因为 HTTP 规范解释了 GET 或 HEAD 请求的有效负载没有定义的语义。

On the API side of the conversation, the server knows about HTTP, so knows how to correctly interpret the bytes of the HTTP request.在对话的 API 端,服务器知道 HTTP,因此知道如何正确解释 HTTP 请求的字节。 Thus, the server knows where to look in the request for the HTTP method, the target URI, the headers that may (or may not) modify the context of the request, and so on.因此,服务器知道在哪里查找 HTTP 方法的请求、目标 URI、可能(或不可能)修改请求上下文的标头,等等。 The implementation can then do whatever it likes with that information, and construct a suitable HTTP response (in effect, lifting into the headers of the response the metadata in a representation that can be understood by all of the general purpose components participating in the conversation.然后,实现可以使用该信息做任何它喜欢的事情,并构造一个合适的 HTTP 响应(实际上,将元数据提升到响应的标头中,以表示所有参与对话的通用组件都可以理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM