【发布时间】:2020-09-17 02:47:32
【问题描述】:
我想通过 MS-Flow 上传到 Amazon s3。通过 python 发布的 http 帖子是这样的
with open('../myfile.txt', 'rb') as f:
files = {'file': (object_name, f)}
http_response = requests.post(
<aws-s3-url>,
data={
'key': 'myfile.txt',
'x-amz-algorithm': 'AWS4-HMAC-SHA256',
'x-amz-credential': '<creds>',
'x-amz-date': '20200529T120357Z',
'policy': '<policy>',
'x-amz-signature': '<signature>'
},
files=files
)
原始请求正文和标头如下所示
--ed1fdd226f0d04d8691a17ceaf914a7e
Content-Disposition: form-data; name="key"
myfile.txt
--ed1fdd226f0d04d8691a17ceaf914a7e
Content-Disposition: form-data; name="x-amz-algorithm"
AWS4-HMAC-SHA256
--ed1fdd226f0d04d8691a17ceaf914a7e
Content-Disposition: form-data; name="x-amz-credential"
<creds>
--ed1fdd226f0d04d8691a17ceaf914a7e
Content-Disposition: form-data; name="x-amz-date"
20200529T120357Z
--ed1fdd226f0d04d8691a17ceaf914a7e
Content-Disposition: form-data; name="policy"
<policy>
--ed1fdd226f0d04d8691a17ceaf914a7e
Content-Disposition: form-data; name="x-amz-signature"
<signature>
--ed1fdd226f0d04d8691a17ceaf914a7e
Content-Disposition: form-data; name="file"; filename="myfile.txt"
test test test
--ed1fdd226f0d04d8691a17ceaf914a7e--
和
{'User-Agent': 'python-requests/2.23.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '1287', 'Content-Type': 'multipart/form-data; boundary=ed1fdd226f0d04d8691a17ceaf914a7e'}
通过 python 发送请求可以正常工作。 但是当我在 MS-Flow 中或通过 curl 使用原始 http-request-body 时,它会失败并显示
The body of your POST request is not well-formed multipart/form-data.
我使用的 curl 命令是
curl \
-d "--ed1fdd226f0d04d8691a17ceaf914a7e\r\nContent-Disposition: form-data; name="key"\r\n\r\nmyfile.txt\r\n--ed1..." \
-X POST \
-H "Content-Type: multipart/form-data" \
-H "boundary: ed1fdd226f0d04d8691a17ceaf914a7e" \
-H "Accept': */*" \
-H "Connection': keep-alive" \
-H "Accept-Encoding': gzip, deflate" \
<aws-s3-url>
我也用过
-d "$(cat body)"
而不是原始字符串。这将换行符从“\r\n”更改为“\n”,但没有帮助。
我的问题有两个:
- 如何从上述 python 请求中正确派生一个有效的 curl 命令? (甚至可能是我需要在 MS-Flow 中输入的正确格式的正文)
- 为什么我的方法不起作用?
如果有人可以提供帮助会很高兴。非常感谢您,祝您有美好的一天!
【问题讨论】:
标签: python http curl python-requests