简体   繁体   English

http:从 python 请求到等效的 curl 命令

[英]http: From python request to equivalent curl command

I want to make an upload to Amazon s3 via MS-Flow.我想通过 MS-Flow 上传到 Amazon s3。 The http post via python looks like this通过 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
        )

The raw request body and headers look as follows原始请求正文和标头如下所示

--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--

and

{'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'}

Sending the request via python works fine.通过 python 发送请求可以正常工作。 But when I use the raw http-request-body in MS-Flow or via curl, it fails with但是当我在 MS-Flow 中或通过 curl 使用原始 http-request-body 时,它会失败

The body of your POST request is not well-formed multipart/form-data.

The curl command I use is我使用的 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>

I also used我也用过

-d "$(cat body)"

instead of the raw string.而不是原始字符串。 This changed the line-breaks from "\r\n" to "\n" but did not help.这将换行符从“\r\n”更改为“\n”,但没有帮助。

My question is twofold:我的问题是双重的:

  1. How do I correctly derive a working curl command from the above python request?如何从上述 python 请求中正确导出有效的 curl 命令? (Possibly even the correct format of the body I would need to enter in MS-Flow) (甚至可能是我需要在 MS-Flow 中输入的正确格式的正文)
  2. Why does my approach not work?为什么我的方法不起作用?

Would be glad if someone could help.如果有人可以提供帮助会很高兴。 Thanks a lot in advance and have a nice day!非常感谢您,祝您有美好的一天!

--data alias -d encodes the given string with URL-encoding. --data alias -d使用 URL 编码对给定的字符串进行编码。 Use --trace - to see the resulting mess that goes out to the server.使用--trace -查看输出到服务器的结果混乱。

If you have already encoded data like this, you need to use --data-binary instead.如果您已经对这样的数据进行了编码,则需要使用--data-binary代替。

You can also use -F key=value to specify each field individually and send them as multipart form data, which is less error-prone because you don't have to do the encoding yourself.您还可以使用-F key=value单独指定每个字段并将它们作为多部分表单数据发送,这样不易出错,因为您不必自己进行编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM