pythonpython-requeststypeform

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

Solution

  • 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!