Python发布请求,发布问题

编程入门 行业动态 更新时间:2024-10-06 20:39:24
本文介绍了Python发布请求,发布问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试编写一个打字机器人,但我完全是一个初学者,所以我在 request.post 上遇到了问题

I'm trying to write a typeform bot but I am a totally beginner so I have problems with request.post

我正在尝试填写此类型表格:typeformtutorial.typeform/to/aA7Vx9通过这段代码

I am trying to fill this typeform: typeformtutorial.typeform/to/aA7Vx9 by this code

import requests token = requests.get("typeformtutorial.typeform/app/form/result/token/aA7Vx9/default") data = {"42758279": "true", "42758410": "text", "token": token} r = requests.post("typeformtutorial.typeform/app/form/submit/aA7Vx9", data) print(r)

我认为数据"有问题,我不确定我是否以一种好的方式使用令牌.你能帮我吗?

I think that something is wrong with "data" and I am not sure if I use token in a good way. Could you help me?

推荐答案

所以,首先,您需要获取另一个带有令牌的字段.为此,您应该在第一个请求中传递标头 'accept': 'application/json'.在响应中,您将获得带有 token 和 landed_at 参数的 json 对象.您应该在下一步中使用它们.

So, first of all, you need to get another field with the token. To do that, you should pass the header 'accept': 'application/json' in your first request. In the response, you'll get the json object with the token and landed_at parameters. You should use them in the next step.

然后,发布数据应该与您传递的数据不同.查看浏览器开发人员工具中的网络选项卡以找出实际模板.它有这样的结构:

Then, the post data shoud be different from what you're passing. See the network tab in the browser's developer tools to find out the actual template. It has a structure like that:

{ "signature": <YOUR_SIGNATURE>, "form_id": "aA7Vx9", "landed_at": <YOUR_LANDED_AT_TIME>, "answers": [ { "field": { "id": "42758279", "type": "yes_no" }, "type": "boolean", "boolean": True }, { "field": { "id": "42758410", "type": "short_text" }, "type": "text", "text": "1" } ] }

最后,您应该将该 json 转换为文本,以便服务器能够成功解析它.

And finally, you should convert that json to text so the server would successfully parse it.

工作示例:

import requests import json token = json.loads(requests.post( "typeformtutorial.typeform/app/form/result/token/aA7Vx9/default", headers={'accept': 'application/json'} ).text) signature = token['token'] landed_at = int(token['landed_at']) data = { "signature": signature, "form_id": "aA7Vx9", "landed_at": landed_at, "answers": [ { "field": { "id": "42758279", "type": "yes_no" }, "type": "boolean", "boolean": True }, { "field": { "id": "42758410", "type": "short_text" }, "type": "text", "text": "1" } ] } json_data = json.dumps(data) r = requests.post("typeformtutorial.typeform/app/form/submit/aA7Vx9", data=json_data) print(r.text)

输出:

{"message":"success"}

更多推荐

Python发布请求,发布问题

本文发布于:2023-10-10 18:12:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!