简体   繁体   中英

Post request to Typeform failing due to Invalid payload

I've been trying to send a POST request to typeform but I keep getting the following output:

{"code":"INVALID_PAYLOAD"}

I have tried the following, but have been unsuccessful in completing the request. The use of headers gave no different result.

How would I correctly format my 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

It seems that you have two issues with your code:

first of all, your signature key in the data dictionary doesn't seem to have the correct value, so I would recommend replacing:

"signature": token,

with

"signature": token.text,

secondly, it seems that typeformtutorial is expecting to receive a JSON string so you should replace:

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

with

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

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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