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

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

问题描述

我知道 Accept 参数定义客户端期望从服务器获得的数据类型。

I understand that the Accept parameter define what is type of data that the client expect from the server.

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

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

推荐答案

阅读相关的RFC。在这种情况下, 7231

Read the relevant RFCs. In this case 7231:


5.3.2。接受

用户代理可以使用接受标题字段来指定
响应媒体类型接受。

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








3.1.1.5。内容类型

内容类型标题字段表示
相关代表的媒体类型

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

所以:接受表示来自服务器的响应客户端可以接受。 Content-type 总是与当前请求或响应的内容有关。

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 use a content-type request header.

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

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