加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Content-Type标头是否适用于除PUT或POST之外的任何HTT

发布时间:2020-12-20 11:16:46 所属栏目:Python 来源:网络整理
导读:我正在尝试使用CollectionSpace软件的REST API,并注意到将其作为GET请求的一部分发送给Content-Type标头会导致以下错误: HTTP Status 415 - Cannot consume content type 我试过的两个python REST客户端库,在谷歌代码上的github和python-rest-client上的res
我正在尝试使用CollectionSpace软件的REST API,并注意到将其作为GET请求的一部分发送给Content-Type标头会导致以下错误:

HTTP Status 415 - Cannot consume content type

我试过的两个python REST客户端库,在谷歌代码上的github和python-rest-client上的restclient,在发出GET请求时都会发送一个Content-Type头.

我对查看http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html的理解是客户端应该只在POST和PUT请求上发送Content-Type标头.那是对的吗?

这两个库都发送了标头的事实使我认为服务器通常会忽略它,而不是返回客户端错误代码.

解决方法

虽然没有在规范中明确概述,但可以做出一些推论. Section 7.2.1州

Any HTTP/1.1 message containing an
entity-body SHOULD include a
Content-Type header field defining the
media type of that body.

这很明显,而且很有意义.鉴于此,我们可以查看Section 9(方法定义),看看哪些提到他们可能在请求体中有一个实体.其中三个提到它:

OPTIONS

If the OPTIONS request includes an
entity-body (as indicated by the
presence of Content-Length or
Transfer-Encoding)…

POST

…used to request that the origin
server accept the entity enclosed in
the request…

…requests that the enclosed entity
be stored under the supplied
Request-URI

并且一种方法特别禁止实体,TRACE:

A TRACE request MUST NOT include an
entity.

实际上,您可以使用正文中的实体和Content-Type标头发送任何方法(TRACE除外).但是,根据规范,我不希望服务器对它做任何事情,除非它是上面三种方法之一.

我还要说,您正在使用的响应HTTP状态415的软件违反了规范.

Section 4.3说:

…if the request method does not
include defined semantics for an
entity-body,then the message-body
SHOULD be ignored when handling the
request.

由于规范不包含具有GET请求的实体主体的已定义语义,因此服务器应忽略它.

另外,如果请求中没有提供实体,并且Content-Length为零(假设Transfer-Encoding标头未设置且不是“identity”),则服务器不应尝试使用实体,无论请求如何方法或是否存在Content-Type标头.这可以通过优先顺序来备份,以确定Section 4.4中描述的消息长度.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读