简体   繁体   English

如何在 python 中发布带有请求的有效负载?

[英]How can I post payload with requests in python?

This is what I observed with f12 in chrome:这是我在 chrome 中使用 f12 观察到的: 镀铬输出

The payload here doesn't seem like json data since there is 89:42 in the front.这里的有效载荷看起来不像 json 数据,因为前面有 89:42。

payload = ["auth",{"form":{"id":"xxxx","email":"xxx@xxx"}}]
resp = requests.post(url, json=payload, headers=headers)
print(resp.status_code)

payload = '89:42["auth",{"form":{"id":"xxxx","email":"xxx@xxx"}}]'
resp = requests.post(url, data=payload, headers=headers)
print(resp.status_code)

The status code of the above resp are both 400(bad request).上述响应的状态码都是 400(bad request)。 How can I post it correctly?我怎样才能正确发布它?

[EDIT] I actually used Session to maintain the session. [编辑] 我实际上使用Session来维护 session。 I'v also tried to change the content-type to application/json .我还尝试将内容类型更改为application/json But it didn't work.但它没有用。 And as your can see in the picture, the default content-type seen with f12 is text/plain .正如您在图片中看到的,使用 f12 看到的默认内容类型是text/plain

[EDIT] Some said the data or json argument must be a dict . [编辑] 有人说datajson参数必须是dict Does it means that I should rewrite 89:42["auth",{"form":{"id":"xxxx","email":"xxx@xxx"}}] to change its type to dict ?这是否意味着我应该重写89:42["auth",{"form":{"id":"xxxx","email":"xxx@xxx"}}]将其类型更改为dict How should I do this?我该怎么做?

From reading the requests documentation , you need to pass your payload as a dictoinary like this:通过阅读请求文档,您需要将有效负载作为字典传递,如下所示:

response = requests.post('https://httpbin.org/post', data = {'key':'value'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM