REST:HTTP标头或请求参数 [英] REST: HTTP headers or request parameters

查看:262
本文介绍了REST:HTTP标头或请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在围绕REST进行一些研究。我注意到 Amazon S3 API主要使用http标头作为其REST接口。这对我来说是一个惊喜,因为我认为接口主要用于请求参数。

I've been putting in some research around REST. I noticed that the Amazon S3 API uses mainly http headers for their REST interface. This was a surprise to me, since I assumed that the interface would work mainly off request parameters.

我的问题是:我应该使用主要的http头开发我的REST接口,或者我应该使用请求参数?

My question is this: Should I develop my REST interface using mainly http headers, or should I be using request parameters?

推荐答案

问题主要是所定义的参数是否是资源标识符(URI)的一部分。如果是,那么你会使用请求参数,否则HTTP自定义标头。例如,在音乐库中传递专辑的I​​D必须是URI的一部分。

The question mainly is whether the parameters defined are part of the resource identifier (URI) or not. if so, then you would use the request parameters otherwise HTTP custom headers. For example, passing the id of the album in a music gallery must be part of the URI.

请记住,例如 / employee / id / 45 (或 / employee?id = 45 ,REST没有偏见针对查询字符串参数或针对清除斜杠分隔的URI)标识一个资源。现在您可以通过发送请求标头 content-type:text / plain content-type:image / jpg 获取信息或图像。在这方面,资源被认为是相同的,标头仅用于定义资源的格式。

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. 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. In this respect, resource is deemed to be the same and header only used to define format of the resource.

通常,我不是HTTP自定义标头的忠实粉丝。 这通常假设客户端具有服务器实现的先验知识(不能通过自然HTTP方式发现,即超媒体),它总是被视为REST反模式

Generally, I am not a big fan of HTTP custom headers. This usually assumes the client to have a prior knowledge of the server implementation (not discoverable through natural HTTP means, i.e. hypermedia) which always is considered a REST anti-pattern

HTTP标头通常将HTTP 正交的各个方面定义为在请求/响应过程中要实现的内容。 授权标题(真的是用词不当,必须是身份验证)是一个典型的例子。

HTTP headers usually define aspects of HTTP orthogonal to what is to be achieved in the process of request/response. Authorization header (really a misnomer, must have been authentication) is a classic example.

这篇关于REST:HTTP标头或请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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