Python请求 - 编码Form -Data

发布于 2025-02-09 02:30:20 字数 687 浏览 0 评论 0原文

我想在请求中复制以下卷曲调用:

curl --request POST 'https://my_api.com/request/dynamic_views' \
--header 'Cookie: session=123' \
--form 'data="{\"searchId\":66966,\"mapAllTeam\":true}"'

我的表单项目有问题,需要作为多部分/form-data提供。

我正在尝试以下(简化的代码):

uri="https://my_api.com/request/dynamic_views"
session_cookie = {"Cookie":"session=123"}
file = {"data": '"{\"searchId\":66966,\"mapAllTeam\":true }"'}

response = requests.post(uri, headers=session_cookie, files=file )

我得到的响应是:

reply: 'HTTP/1.1 400 \r\n'
[...]
{'status': 'Success', 'message': 'Missing required data, Please enter proper Data.'}

这表明表格已被错误附加。

I would like to replicate following curl call in requests:

curl --request POST 'https://my_api.com/request/dynamic_views' \
--header 'Cookie: session=123' \
--form 'data="{\"searchId\":66966,\"mapAllTeam\":true}"'

I have a problem with form item which need to be provided as multipart/form-data.

I'm trying the following(simplified code):

uri="https://my_api.com/request/dynamic_views"
session_cookie = {"Cookie":"session=123"}
file = {"data": '"{\"searchId\":66966,\"mapAllTeam\":true }"'}

response = requests.post(uri, headers=session_cookie, files=file )

Response I got is:

reply: 'HTTP/1.1 400 \r\n'
[...]
{'status': 'Success', 'message': 'Missing required data, Please enter proper Data.'}

Which indicates that form has been incorrectly attached.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

并安 2025-02-16 02:30:20

它必须看起来像

files = {'data': (None,'{"searchId":66966,"mapAllTeam":true}')}

It has to look like

files = {'data': (None,'{"searchId":66966,"mapAllTeam":true}')}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文