gpt4 book ai didi

当在请求中使用 "data"时,python 请求会丢弃 "header"

转载 作者:太空宇宙 更新时间:2023-11-03 11:40:41 25 4
gpt4 key购买 nike

我正在处理网站的 REST API,当我想上传文件时需要这种请求类型:

  1. 标题中的“授权”和多部分内容类型
  2. 以二进制字符串的形式(正文)归档
  3. 请求 URL 中的文件类型

所以我做了这段代码:

import requests

url = 'http://httpbin.org/post'

parameters = {
'format': 'pdf',
}

headers = {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json',
'Authorization' : 'Some authorization code'
}

data = {'file': open('1.pdf', 'rb')}

r = requests.post(url, params=parameters, headers=headers, data=data)

print(r.text)

但是请求似乎正在丢弃数据:

{
"args": {
"format": "pdf"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate",
"Authorization": "Some authorization code",
"Connection": "close",
"Content-Length": "30",
"Content-Type": "multipart/form-data",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.1"
},
"json": null,
"origin": "x.x.x.x",
"url": "http://httpbin.org/post?format=pdf"
}

当我删除请求中的“ header ”部分时,它会起作用:

r = requests.post(url, params=parameters, data=data)

因为响应是:

{
"args": {
"format": "pdf"
},
"data": "",
"files": {},
"form": {
"fax_file": "some samplae texts\n"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "30",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.1"
},
"json": null,
"origin": "x.x.x.x",
"url": "http://httpbin.org/post?format=pdf"
}

我也尝试过准备好的请求,结果是一样的。

最佳答案

您正在尝试发布文件数据,因此请使用files 选项:

r = requests.post(url, params=parameters, files=data, headers=headers)

但是,您真的不应该设置 Content-Type header ;当您使用 files 选项时,它会为您设置。本例中的 header 包含字段边界,以真正希望库为您处理此问题:

headers = {
'Accept': 'application/json',
'Authorization' : 'Some authorization code'
}

如果保留 Content-Type header ,则必须预先生成内容主体,以便能够向接收服务器提供所需的边界信息。

您也可以尝试删除 Accept header ;默认情况下,requests 将添加 Accept: */* 如果您未指定该 header ,则表明任何都是可接受的。

仅使用data参数时,参数编码为application/x-www-form-urlencoded形式,不支持大文件数据,并且您的 Content-Type header 与实际的 POST 正文内容不匹配。

参见 Post a Multipart-Encoded Filerequests 文档和 application/x-www-form-urlencoded or multipart/form-data? 中在 Stack Overflow 上。

演示:

>>> import requests
>>> url = 'http://httpbin.org/post'
>>> parameters = {'format': 'pdf'}
>>> headers = {
... 'Accept': 'application/json',
... 'Authorization' : 'Some authorization code',
... }
>>> data = {'file': open('1.pdf', 'rb')}
>>> r = requests.post(url, params=parameters, files=data, headers=headers)
>>> print(r.text)
{
"args": {
"format": "pdf"
},
"data": "",
"files": {
"file": "<file data as base64>"
},
"form": {},
"headers": {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate",
"Authorization": "Some authorization code",
"Cache-Control": "max-age=0",
"Connection": "close",
"Content-Length": "374751",
"Content-Type": "multipart/form-data; boundary=d4b84f8bfd464e3f97e3de584d7315fc",
"Host": "httpbin.org",
"O2Gw-Id": "03",
"User-Agent": "python-requests/2.18.4",
"X-Gateway": "wap.london.02.net"
},
"json": null,
"origin": "10.120.6.78, 82.132.221.209",
"url": "http://httpbin.org/post?format=pdf"
}

注意 multipart/form-data; boundary=d4b84f8bfd464e3f97e3de584d7315fc Content-Type header 的值!

关于当在请求中使用 "data"时,python 请求会丢弃 "header",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49996188/

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