簡體   English   中英

http:從 python 請求到等效的 curl 命令

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

我想通過 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”,但沒有幫助。

我的問題是雙重的:

  1. 如何從上述 python 請求中正確導出有效的 curl 命令? (甚至可能是我需要在 MS-Flow 中輸入的正確格式的正文)
  2. 為什么我的方法不起作用?

如果有人可以提供幫助會很高興。 非常感謝您,祝您有美好的一天!

--data alias -d使用 URL 編碼對給定的字符串進行編碼。 使用--trace -查看輸出到服務器的結果混亂。

如果您已經對這樣的數據進行了編碼,則需要使用--data-binary代替。

您還可以使用-F key=value單獨指定每個字段並將它們作為多部分表單數據發送,這樣不易出錯,因為您不必自己進行編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM