标头参数:“接受"和“内容类型"在 REST 上下文中 [英] Header parameters: "Accept" and "Content-type" in a REST context

查看:27
本文介绍了标头参数:“接受"和“内容类型"在 REST 上下文中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Accept 参数定义了从服务器发送的客户端响应中预期的数据类型,因此它用作响应标头.

我的问题是关于 Content-type,客户端使用它来定义发送请求的正文格式,我总是将它用作客户端请求的一部分,所以我有一个客户端请求我使用 AcceptContent-type 设置标头.最近,我遇到了一个项目,其中 Content-type 在响应头中定义(因此由服务器发送).所以我的问题是:Content-type 需要设置为客户端请求标头的一部分或服务器响应标头的一部分,还是可以同时设置?

解决方案

阅读相关的 RFC.在这种情况下 7231:

<块引用>

5.3.2.接受

接受"用户代理可以使用标头字段来指定响应可接受的媒体类型.


<块引用>

3.1.1.5.内容类型

内容类型"标头字段指示媒体类型关联表示

所以:Accept 表示客户端可以接受什么样的来自服务器的响应.Content-type 总是关于当前请求或响应的内容.

因此,如果您的请求没有有效负载,您就不必使用内容类型的请求标头.

服务器可能会要求您在请求中提供内容类型,即使请求没有有效负载;如果您省略它,服务器将返回 415 Unsupported Media Type 响应.

I understand that the Accept parameter define a data type expected in a client response sent from the server, so it's used as a response header.

My question is regarding the Content-type, it's used by a client to define the body format of a request sent, I always used it as part of a client request, so I have a client request where I set the headers with Accept and Content-type. And recently, I came across a project where the Content-type is defined in the response headers (so sent by the server). So my question is: Content-type need to be set as part of the client request header or as part of the server response header or can it be set to both ?

解决方案

Read the relevant RFCs. In this case 7231:

5.3.2. Accept

The "Accept" header field can be used by user agents to specify response media types that are acceptable.


3.1.1.5. Content-Type

The "Content-Type" header field indicates the media type of the associated representation

So: Accept indicates what kind of response from the server the client can accept. Content-type always is about the content of the current request or response.

So if your request has no payload, you don't have to use a content-type request header.

Servers may require you to provide a content-type in a request even if the request has no payload; the sever will return a 415 Unsupported Media Type response if you omit it.

这篇关于标头参数:“接受"和“内容类型"在 REST 上下文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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