为什么我的HTTP POST(二进制文件)使用Python请求但不使用cURL?

时间:2018-01-15 05:30:22

标签: python http curl binary python-requests

我正在尝试通过HTTP POST请求将二进制文件(.parquet)发送到Web服务器。下面的python代码完美无缺:

url = 'http://localhost/update/id'
f = open('/home/dev/datafiles/file.parquet', 'rb')
files = {'file': f}
try:
    r = requests.post(url, files=files)
finally:
    f.close()

然而,使用cURL,我无法使其工作,导致错误400:错误请求。我正在尝试的cURL命令是:

curl -X POST \
-H 'Content-Type: multipart/form-data' \
--data-binary @/home/dev/datafiles/file.parquet \
http://localhost/update/id

我尝试使用-F代替-X POST-d而不是--data-binary,但无论如何我都会获得400分。

使用curlify从Python中的PreparedRequest对象中提取cURL命令,我得到:

curl -X POST \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Accept: */*' \
-H 'Connection: keep-alive' \
-H 'Content-Length: 85705' \
-H 'Content-Type: multipart/form-data; boundary=c732fde3afb641d2ba5010efc59497bd' \
-H 'User-Agent: python-requests/2.18.4' \
-d '--c732fde3afb641d2ba5010efc59497bd Content-Disposition: form-data; name="file"; filename="file.parquet" [binary_content_goes_here] --c732fde3afb641d2ba5010efc59497bd--' \
http://localhost/update/id

但即使用-d-d @/home/dev/datafiles/file.parquet替换--data-binary @/home/dev/datafiles/file.parquet部分,我也无法使其发挥作用。

我错过了什么吗?

0 个答案:

没有答案