【问题标题】:Issue with passing parameters with python requests使用 python 请求传递参数的问题
【发布时间】:2020-02-10 02:11:28
【问题描述】:

我曾尝试使用 python-requests 使用 post 方法从网站获取数据。但是在检查请求有效负载时,它显示如下:

Content-Type: multipart/form-data; boundary=---------------------------7952717927472
Content-Length: 5372

-----------------------------7952717927472
Content-Disposition: form-data; name="id"

13529
-----------------------------7952717927472
Content-Disposition: form-data; name="num"


-----------------------------7952717927472
Content-Disposition: form-data; name="dowhat"

error
-----------------------------7952717927472
Content-Disposition: form-data; name="coupoun"

$ 2.00
-----------------------------7952717927472

我不明白请求负载的格式以及该负载必须如何与请求一起传递。

response=requests.post(url, data = data)

【问题讨论】:

标签: python-3.x python-requests


【解决方案1】:

是的,我终于明白了。请参阅下面使用 MultiparEncoder 的代码

from requests_toolbelt import MultipartEncoder
import requests

m = MultipartEncoder(
fields={'field0': 'value', 'field1': 'value',
        'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
)

r = requests.post('http://httpbin.org/post', data=m,
              headers={'Content-Type': m.content_type})

这很好用

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 2018-02-27
    • 2012-11-14
    • 1970-01-01
    • 2020-11-08
    相关资源
    最近更新 更多