Skip to content Skip to sidebar Skip to footer

Python Post Request, Problem With Posting

I'm trying to write a typeform bot but I am a totally beginner so I have problems with request.post I am trying to fill this typeform: https://typeformtutorial.typeform.com/to/aA7V

Solution 1:

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"
        }
    ]
}

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

Working example:

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)

Output:

{"message":"success"}

Post a Comment for "Python Post Request, Problem With Posting"