1

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/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?

asked Oct 28, 2019 at 23:22
2
  • Can you please share with us what is the result and why do thing that there is something wrong. Commented Oct 28, 2019 at 23:27
  • Try adding > data=json.dumps(data) Like this r = requests.post("typeformtutorial.typeform.com/app/form/submit/aA7Vx9", data=json.dumps(data)) Commented Oct 28, 2019 at 23:42

1 Answer 1

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"}
answered Oct 29, 2019 at 0:08
Sign up to request clarification or add additional context in comments.

1 Comment

It looks like it doesn't work now. Please tell me how to get a token from typeform now?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.