python-3.x - 如何在 python 中的 http 请求中提供 Form-param、值和文件

标签 python-3.x http

我想发出一个 http 请求,如下面的 curl 命令所示:

curl -X PUT \
https://anypoint.mulesoft.com/cloudhub/api/v2/applications/highfiles \
-H 'authorization: Bearer XXX' \
-H 'cache-control: no-cache' \
-H 'content-length: 0' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'host: anypoint.mulesoft.com' \
-H 'postman-token: XXX' \
-H 'x-anypnt-env-id: XXX' \
-H 'x-anypnt-org-id: XXX' \
-F 'appInfoJson={
"muleVersion": {
    "version": "3.8.5"
},
"properties":{"env":"dev"}
}'

我已经尝试了下面的请求,但都是徒劳的

files = {'file': open('C:\Users\\highfiles.zip', 'rb')}
    appInfoJson1 = {
        "muleVersion": {
            "version": "3.8.5"
        },
        "properties": {"env":"dev1"}
    }
    print dict(appInfoJson=appInfoJson1)
    headers = {"X-ANYPNT-ORG-ID": "XXXX",
               "X-ANYPNT-ENV-ID": "XXXX",
               "Authorization": "Bearer " + access_token,
               }
    response = requests.put("https://anypoint.mulesoft.com/cloudhub/api/v2/applications/highfiles",
                            data=dict(appInfoJson=appInfoJson1) , files=files, headers = headers)

如何在 python http 请求中提供表单参数值和文件。

最佳答案

我做错了。

变化只是 w.r.t 处理字典值,如下所示:

response = requests.put("https://anypoint.mulesoft.com/cloudhub/api/v2/applications/highfiles ", data=dict(appInfoJson=appInfoJson1.values()) , files=文件, headers = headers)

关于python-3.x - 如何在 python 中的 http 请求中提供 Form-param、值和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46110504/

相关文章:

python - 将 dict 字段转换为 **list** 以在 **enumarate** 中使用

http - 浏览器如何将网络响应映射回请求?

python - 有没有一种简单的方法可以区分运行 Fedora 12 的两个系统之间的网络设置差异?

http - 我们可以使用 TLS over TCP 协议(protocol)吗?

Python 2 与 3 正则表达式的区别

python-3.x - 语法错误: unexpected EOF while parsing - How to solve?

python - 使用 Python 来 PUT PMML

python - 如何使用网格移动图像?

rest - 如何在 Robot Framework 中使用 REST 库?

java: 让 Apache HttpClient 连接到具有特定 IP 的主机