pythonrequesttypeform

error with signature token when filling a typeform


I'm learning Python and I'm trying to fill a type form using Python requests, however I get this error response:

{"code":"VALIDATION_ERROR","description":"The provided body is malformed.","details":[{"code":"INVALID_PAYLOAD","description":"signature mismatch","in":"BODY","field":".signature"}]}

However, when I put the token.text value manually in data it works. I can't get it working when adding the token programmatically to the payload, even tried with str() and it's not working.

Here is the code:

import requests
import time

epochTime = int(time.time())
token = requests.get("https://mehdikhireddine.typeform.com/app/form/result/token/OcotDC/default")
final = token.text
print(token.text)
data = { "signature": final,
         "form_id": "OcotDC",
         "landed_at": epochTime,""
         "answers": [
        {
            "field": {
                "id": "nDoisfzMsrBP",
                "type": "short_text"
            },
            "type": "text",
            "text": "mehdilemoi"
        },
        {
            "field": {
                "id": "iCW4s6Fc37OL",
                "type": "legal"
            },
            "type": "boolean",
            "boolean": True
        }

    ]
}
r = requests.post("https://mehdikhireddine.typeform.com/app/form/submit/OcotDC", json=data)
print(r.text)

Solution

  • This issue arises if you store the epochTime before getting the token from the HTTPS response, presumably because the server then fails to match the signature and time, resulting in "signature mismatch".

    You can fix this by taking the current time after the token response has been received, i.e.:

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

    Output:

    {"message":"success"}
    

    Note that this may still intermittently fail if too much time elapses between getting the response and getting the epochTime.