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

安德烈斯特

我想通过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别名-d使用URL编码对给定的字符串进行编码。使用--trace -看所产生的混乱,出去到服务器。

如果您已经编码了这样的数据,则需要使用--data-binary

您还可以-F key=value单独指定每个字段,然后将它们作为多部分表单数据发送,这样就不容易出错,因为您不必自己进行编码。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

等效于CURL命令的Python

来自分类Dev

cURL POST请求无法与python请求等效

来自分类Dev

等效于curl命令

来自分类Dev

cURL到PHP等效

来自分类Dev

Python请求中的CURL -T(等效于--upload-file)

来自分类Dev

python pycurl等效于curl -b get命令

来自分类Dev

python pycurl等效于curl -b get命令

来自分类Dev

用于 https 调用的 curl 命令的等效 python 代码

来自分类Dev

WebRequest等效于CURL命令

来自分类Dev

PowerShell中的cURL命令等效

来自分类Dev

PowerShell中的cURL命令等效

来自分类Dev

WebClient等效于CURL命令

来自分类Dev

Ruby:将curl命令转换为Ruby net / http请求

来自分类Dev

通过命令行和curl发送HTTP请求

来自分类Dev

http帖子请求curl

来自分类Dev

什么是python请求,等同于以下curl命令

来自分类Dev

将curl命令转换为python pycurl或请求

来自分类Dev

请求Python和Curl命令不起作用

来自分类Dev

使用请求将 cURL 命令转换为 python

来自分类Dev

HTTP GET的cURL命令

来自分类Dev

什么与在Java中跟随curl命令等效

来自分类Dev

Ruby等效于该curl命令

来自分类Dev

Python Flask返回反斜杠到JSON curl请求

来自分类Dev

为curl请求建立一个等效的R请求

来自分类Dev

如何运行与“ wget”命令类似的等效“ curl”命令?

来自分类Dev

当通过命令行使用curl时,HAproxy不会将请求从http转发到https

来自分类Dev

如何将cURL命令转换为angular中的http请求?

来自分类Dev

我如何将curl命令转换为AngularJS $ http请求?

来自分类Dev

使用curl模拟HTTP请求