简体   繁体   English

REST:HTTP标头或请求参数

[英]REST: HTTP headers or request parameters

I've been putting in some research around REST. 我一直在围绕REST进行一些研究。 I noticed that the Amazon S3 API uses mainly http headers for their REST interface. 我注意到Amazon S3 API主要使用http标头作为其REST接口。 This was a surprise to me, since I assumed that the interface would work mainly off request parameters. 这对我来说是一个惊喜,因为我认为接口主要用于请求参数。

My question is this: Should I develop my REST interface using mainly http headers, or should I be using request parameters? 我的问题是:我应该主要使用http标头开发我的REST接口,还是应该使用请求参数?

The question mainly is whether the parameters defined are part of the resource identifier (URI) or not. 问题主要在于定义的参数是否是资源标识符(URI)的一部分。 if so, then you would use the request parameters otherwise HTTP custom headers. 如果是这样,那么你将使用请求参数,否则使用HTTP自定义标头。 For example, passing the id of the album in a music gallery must be part of the URI. 例如,在音乐库中传递album的ID必须是URI的一部分。

Remember, for example /employee/id/45 (Or /employee?id=45 , REST does not have a prejudice against query string parameters or for clean slash separated URIs) identifies one resource. 请记住,例如/employee/id/45 (或者/employee?id=45 ,REST 查询字符串参数没有偏见或者对于清晰的斜杠分隔的URI)标识一个资源。 Now you could use content-negotiation by sending request header content-type: text/plain or content-type: image/jpg to get the info or the image. 现在,您可以通过发送请求标头content-type: text/plain来使用内容协商content-type: text/plaincontent-type: image/jpg来获取信息或图像。 In this respect, resource is deemed to be the same and header only used to define format of the resource. 在这方面,资源被认为是相同的,并且标头仅用于定义资源的格式。

Generally, I am not a big fan of HTTP custom headers. 一般来说,我不是HTTP自定义标头的忠实粉丝。 This usually assumes the client to have a prior knowledge of the server implementation (not discoverable through natural HTTP means, ie hypermedia) which always is considered a REST anti-pattern 这通常假设客户端具有服务器实现的先验知识(不能通过自然HTTP方式发现,即超媒体),这通常被认为是REST反模式

HTTP headers usually define aspects of HTTP orthogonal to what is to be achieved in the process of request/response. HTTP标头通常定义与请求/响应过程中要实现的HTTP 正交的HTTP方面。 Authorization header (really a misnomer, should have been authentication) is a classic example. Authorization标题(真的是用词不当,应该是身份验证)是一个典型的例子。

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

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