简体   繁体   English

为什么请求库会在我设置的标题中添加额外的标题?

[英]Why does the requests library add extra headers to the ones I set?

I am trying to do a post request on python utilizing the requests library, when I set my custom headers which are the following: 当我设置以下自定义标头时,我尝试使用请求库在python上进行发布请求:

User-Agent: MBAM-C
Content-Type: application/json
Authorization: True
Content-Length: 619
Connection: Close

However when It send the request with the custom headers it adds its own headers which give a bad request response from the server.. 但是,当它使用自定义标头发送请求时,它会添加自己的标头,从而导致服务器的请求响应不正确。

User-Agent: MBAM-C
Accept-Encoding: gzip, deflate
Accept: */*
Connection: Close
Content-Type: application/json
Authorization: True
Content-Length: 559

It is due to the design goals of the requests project. 这是由于请求项目的设计目标所致。

This behavior is documented here . 此行为在此处记录 You may want to use a lower level library, if it is problematic for the library to be correcting content length or adding desirable headers. 如果该库需要更正内容长度或添加所需的标头,则可能需要使用较低级别的库。 Requests bills itself as: "an elegant and simple HTTP library for Python, built for human beings." 要求帐单本身为:“一个为人类而建的优雅而简单的Python HTTP库。” and a part of that is advertising that it can accept compressed content and all mime types. 广告的一部分是它可以接受压缩内容和所有mime类型。

Note: Custom headers are given less precedence than more specific sources of information . 注意: 自定义标头的优先级低于更具体的信息源 For instance: 例如:

Authorization headers set with headers= will be overridden if credentials are specified in .netrc, which in turn will be overridden by the auth= parameter. 如果在.netrc中指定了凭据,则将覆盖标有headers =的授权标头,而auth =参数将覆盖该标头。 Authorization headers will be removed if you get redirected off-host. 如果您脱离主机重定向,则将删除授权标头。 Proxy-Authorization headers will be overridden by proxy credentials provided in the URL. URL中提供的代理凭证将覆盖Proxy-Authorization标头。 Content-Length headers will be overridden when we can determine the length of the content . 当我们可以确定content的长度时,Content-Length标头将被覆盖

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM