由于有效负载无效,将请求发布到Typeform失败

时间:2019-11-04 09:17:56

标签: python python-requests typeform

我一直在尝试向typeform发送POST请求,但是我一直得到以下输出:

{"code":"INVALID_PAYLOAD"}

我尝试了以下操作,但未能成功完成请求。标头的使用没有不同的结果。

我如何正确格式化有效载荷?

import requests
import time

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

data = {
    "signature": token,
    "form_id": "aA7Vx9",
    "landed_at": epochTime,
    "answers": [
        {
            "field": {
                "id": "42758279",
                "type": "yes_no"
            },
            "type": "boolean",
            "boolean": True
        },
        {
            "field": {
                "id": "42758410",
                "type": "short_text"
            },
            "type": "text",
            "text": "Hi"
        }
    ]
}
r = requests.post("https://typeformtutorial.typeform.com/app/form/submit/aA7Vx9",
    data=data)
print(r.text) #.... INVALID_PAYLOAD

1 个答案:

答案 0 :(得分:1)

似乎您的代码有两个问题:

首先,您在signature词典中的data键似乎没有正确的值,因此我建议您替换:

"signature": token,

使用

"signature": token.text,

其次,似乎typeformtutorial期望接收JSON字符串,因此您应该替换:

requests.post("https://typeformtutorial.typeform.com/app/form/submit/aA7Vx9",data=data)

使用

requests.post("https://typeformtutorial.typeform.com/app/form/submit/aA7Vx9",json=data)

希望这会有所帮助!