gpt4 book ai didi

python - 如何在python下使用requests发出post文件请求?

转载 作者:行者123 更新时间:2023-12-01 01:43:14 25 4
gpt4 key购买 nike

我已经使用multipart/form-data发出了一个帖子请求; border=a1c2469c-2a1f-48e6-8f1d-311f8650c855 我明白了

POST /api/feed/upload-resource HTTP/1.1
App-Id: 15762288
Version-Name: 3.26.0.451
User-Agent: Right-Android/3.26.0.451
Content-Type: multipart/form-data; boundary=a1c2469c-2a1f-48e6-8f1d-311f8650c855
Content-Length: 75412
Connection: Keep-Alive
Accept-Encoding: gzip

--a1c2469c-2a1f-48e6-8f1d-311f8650c855
Content-Disposition: form-data; name="h"
Content-Length: 4

1080
--a1c2469c-2a1f-48e6-8f1d-311f8650c855
Content-Disposition: form-data; name="w"
Content-Length: 4

1080
--a1c2469c-2a1f-48e6-8f1d-311f8650c855
Content-Disposition: form-data; name="image"; filename="/storage/emulated/0/Android/data/com.xiaoyu.rightone/tiny/tiny-738-2018-08-03-15-32-53.jpg"
Content-Type: text/plain
Content-Length: 74910

如果我想使用Python requests包模拟这个请求我该怎么做。

我检查了文档,并且尝试过使用这样的文件参数

with open(path, 'rb') as f:
files = {
"h": "1495",
"w": "840",
"filename": ("image", f.read()),
}
r = requests.post(url, files=files)

但是在我的情况下,我总是收到类似 upload_resource_empty 的错误。

最佳答案

这应该有效:

import requests

files = {
'image': ('file_name.jpg', open('file.jpg', 'rb'), 'text/plain'),
'w': (None, '123'),
'h': (None, '222')
}

response = requests.post('url', files=files)

字典中的元组具有以下格式:('filename', fileobj, 'content_type', custom_headers)

关于python - 如何在python下使用requests发出post文件请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51669488/

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