0

在服务器端(RESTful 服务器),决定如何读取输入参数和数据的主要标准是什么?

Accept或者Content-Type

如果“客户端”(JS)向我的 RESTful 服务发送 POST 请求和编码为通常表单数据的数据(应该保存的内容),他应该使用什么样的标题来帮助我如何读取他的数据以及发送什么样的答案他回来了?

4

1 回答 1

1

内容类型

Content-Type specifies the media type of the underlying data.
....
Any HTTP/1.1 message containing an entity-body SHOULD include a
Content-Type header field defining the media type of that body. If
and only if the media type is not given by a Content-Type field, the
recipient MAY attempt to guess the media type via inspection of its
content and/or the name extension(s) of the URI used to identify the
resource. If the media type remains unknown, the recipient SHOULD
treat it as type "application/octet-stream".

如果客户端发送 HTML 表单数据,则正确的请求内容类型标头是 application/x-www-url-form-encoded 或 multipart/form-data。它取决于 HTML 表单的 enctype 属性。

接受

 The Accept request-header field can be used to specify certain media
 types which are acceptable for the response.

因此,服务器最终使用 Content-Type 请求标头来决定传入请求正文的表示形式,并使用 Accept 请求标头来决定将什么表示形式作为响应正文发送回客户端。请注意,响应也应包含 Content-Type 标头,但它仅表示已选择的响应主体的表示。

https://www.rfc-editor.org/rfc/rfc2616

于 2013-04-12T12:51:17.873 回答