Python发布请求,发布问题 [英] Python post request, problem with posting

查看:66
本文介绍了Python发布请求,发布问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

import requests

token = requests.get("https://typeformtutorial.typeform.com/app/form/result/token/aA7Vx9/default")

data = {"42758279": "true",
        "42758410": "text",
        "token": token}

r = requests.post("https://typeformtutorial.typeform.com/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'.在响应中,您将获得带有 tokenlanded_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(
    "https://typeformtutorial.typeform.com/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("https://typeformtutorial.typeform.com/app/form/submit/aA7Vx9", data=json_data)

print(r.text)

输出:

{"message":"success"}

这篇关于Python发布请求,发布问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆