Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 请求删除内容类型头_Python_Python 3.x_Http_Post - Fatal编程技术网

Python 请求删除内容类型头

Python 请求删除内容类型头,python,python-3.x,http,post,Python,Python 3.x,Http,Post,我想从POST请求中删除内容类型标题。我已尝试将标题设置为' 但这发出了: { // ... "headers": { "Content-Type": "", // ... } // ... } 我希望它做的是不发送任何内容类型,而不是发送一个空白的。默认情况下,它是application/x-www-form-urlencoded 这可以通过请求轻松实现: print(requests.post(url, test_data).text) 但这是我需要分发的脚本,

我想从POST请求中删除内容类型标题。我已尝试将标题设置为
'

但这发出了:

{
  // ...
  "headers": {
     "Content-Type": "", // ...
  }
  // ...
}
我希望它做的是不发送任何内容类型,而不是发送一个空白的。默认情况下,它是
application/x-www-form-urlencoded

这可以通过
请求轻松实现:

print(requests.post(url, test_data).text)

但这是我需要分发的脚本,因此不能有依赖项。我需要它没有任何内容类型,因为服务器非常挑剔,所以我不能使用
text/plain
application/octet-stream

您可以指定自定义处理程序:

try:
    from urllib.request import Request, urlopen, build_opener, BaseHandler
except ImportError:
    from urllib2 import Request, urlopen, build_opener, BaseHandler

url = 'https://httpbin.org/post'
test_data = 'test'

class ContentTypeRemover(BaseHandler):
    def http_request(self, req):
        if req.has_header('Content-type'):
            req.remove_header('Content-type')
        return req
    https_request = http_request

opener = build_opener(ContentTypeRemover())
req = Request(url, test_data.encode())
print(opener.open(req).read().decode())

另一种(黑客)方法:猴子修补请求对象,假装已经存在
内容类型
头;防止将
AbstractHTTPHandler
设置为默认内容类型标题

req = Request(url, test_data.encode())
req.has_header = lambda header_name: (header_name == 'Content-type' or
                                      Request.has_header(req, header_name))
print(urlopen(req).read().decode())

加上@falsetru的答案

如果您需要筛选更多的标题,则您不需要使用
req.headers
,它更像这样:

类ContentTypeRemover(BaseHandler):
def uu init(self,headers_to_filter={'Content-type'}):#set或dict有效
self.headers\u to\u filter=headers\u to\u filter
def http_请求(self,req):
对于标头,请求标头_items()中的值:
如果self.headers\u to\u过滤器中的标题:
请求删除标题(标题)
返回请求
https\u请求=http\u请求
如果您需要修改标题,请不要删除它。。这有点奇怪(至少这是我发现唯一有效的方法):

req.remove_头(头)
请求添加_头(头,self.modify_头[header.lower()]))

第一种方法似乎只适用于Python 3,因为Python 2没有
req.remove\u header
方法。第二种方法在这两种情况下都有效,并且可以通过创建子类来减少黑客攻击。我也可以用一个子类发布它。谢谢@Artyer,它之所以有黑客行为,是因为它没有文档记录,并且依赖于实现细节。@Artyer,在Python 2.x中,
req.headers.pop('Content-type',None)
req.unredired\u hdrs.pop('Content-type',None)
而不是
req.remove\u header(…)
。该
lambda
中的主体可以重写为
标题\u name=='Content type'或Request.has\u标题(req,header\u name)
。请注意,对该表达式的求值将在
处停止,如果第一部分的求值结果已为
True
@uplie,则您是对的。谢谢你的评论。我会相应地更新答案。
req = Request(url, test_data.encode())
req.has_header = lambda header_name: (header_name == 'Content-type' or
                                      Request.has_header(req, header_name))
print(urlopen(req).read().decode())