gpt4 book ai didi

http - 哪些 HTTP 方法需要主体?

转载 作者:可可西里 更新时间:2023-11-01 15:05:30 24 4
gpt4 key购买 nike

一些 HTTP 方法,例如 POST,需要在 header 和双 CRLF 之后发送正文。

其他的,例如GET,没有正文,对它们来说,双CRLF 标志着请求的结束。

但是其他的呢:PUTDELETE,...如何知道哪个需要主体?

通用 HTTP 客户端应该如何对未知的 HTTP 方法使用react?拒绝吗?默认需要正文,还是默认不需要正文?

如能提供相关规范的指针,我们将不胜感激。


编辑:我会在评论中详细说明我的问题。

我正在设计一个通用的 HTTP 客户端,程序员可以使用它向任何服务器发送任意 HTTP 请求。

客户端可以这样使用(伪代码):

HttpClient.request(method, url [, data]);

数据是可选的,可以是原始数据(字符串),也可以是键/值对的关联数组。

如果数据是数组,库会对数据进行 url 编码,然后将数据附加到 URL 以进行 GET 请求,或者将其发送到消息正文中以进行 POST 请求。

因此,鉴于开发人员选择的 HTTP 方法,我正在尝试确定此 HttpClient 是否必须/应该/不得/不应在请求中包含消息正文。

最佳答案

编辑:编译列表:

  • entity-body 仅在 message-body 出现时出现(第 7.2 节)
  • message-body 的存在通过包含 Content-LengthTransfer-Encoding header 来表示(第 4.3 节)
  • 当请求方法的规范不允许发送实体主体时,不得包含消息主体(第 4.3 节)
  • entity-body 仅在 TRACE 请求中被明确禁止,所有其他请求类型不受限制(第 9 节,特别是 9.8)

对于响应,这已被定义:

  • 是否包含消息正文取决于请求方法响应状态(第 4.3 节)
  • 在响应 HEAD 请求时明确禁止消息正文(第 9 节,特别是 9.4)
  • 在 1xx(信息)、204(无内容)和 304(未修改)响应(第 4.3 节)中明确禁止消息正文
  • 所有其他响应都包含消息正文,尽管它的长度可能为零(第 4.3 节)

This (RFC 7231) 或 This版本(来自 IETF & More In-Depth)就是你想要的。根据 RFC:

对于 PUT:

The PUT method requests that the enclosed entity be stored under thesupplied Request-URI. If the Request-URI refers to an already existingresource, the enclosed entity SHOULD be considered as a modifiedversion of the one residing on the origin server. If the Request-URIdoes not point to an existing resource, and that URI is capable ofbeing defined as a new resource by the requesting user agent, theorigin server can create the resource with that URI. If a new resourceis created, the origin server MUST inform the user agent via the 201(Created) response. If an existing resource is modified, either the200 (OK) or 204 (No Content) response codes SHOULD be sent to indicatesuccessful completion of the request. If the resource could not becreated or modified with the Request-URI, an appropriate errorresponse SHOULD be given that reflects the nature of the problem. Therecipient of the entity MUST NOT ignore any Content-* (e.g.Content-Range) headers that it does not understand or implement andMUST return a 501 (Not Implemented) response in such cases.

对于 DELETE:

The DELETE method requests that the origin server delete the resourceidentified by the Request-URI. This method MAY be overridden by humanintervention (or other means) on the origin server. The client cannotbe guaranteed that the operation has been carried out, even if thestatus code returned from the origin server indicates that the actionhas been completed successfully. However, the server SHOULD NOTindicate success unless, at the time the response is given, it intendsto delete the resource or move it to an inaccessible location.

A successful response SHOULD be 200 (OK) if the response includes anentity describing the status, 202 (Accepted) if the action has not yetbeen enacted, or 204 (No Content) if the action has been enacted butthe response does not include an entity.

If the request passes through a cache and the Request-URI identifiesone or more currently cached entities, those entries SHOULD be treatedas stale. Responses to this method are not cacheable.

关于http - 哪些 HTTP 方法需要主体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16339198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com