curltypeform

CUrl download result image from typeform results in log in page


I'm rather new at using either curl or the typeform API, so not just answers are appreciated but also any links or tutorials to help me on my way.

The problem I am facing is that when I try to get my uploaded image on typeform using either curl or wget, it does not save the image but rather the log in page from typeform

https://admin.typeform.com/login/

Recovering the download link itself using the API works fine. I used the command

curl --request GET --url https://api.typeform.com/forms/aLsAcC/responses --header 'Authorization: Bearer {MyId}'

This will return a JSON object with all responses. Inside this JSON file is the following block:

"field":{  
       "id":"SM3co23sAfCQ",  
        "type":"file_upload",  
        "ref":"2953330e-3c0b-4da3-bbb1-78a370152b10"  
        },  
"type":"file_url",  
"file_url":"https://admin.typeform.com/form/aLsAcC/field/SM3co23sAfCQ/results/1a4a6ab57b29-IMG_0354.JPG/download"  

But then when I try to actually download the file it doesn't seem to work. It doesn't matter if I run:

curl -O https://admin.typeform.com/form/aLsAcC/field/SM3co23sAfCQ/results/1a4a6ab57b29-IMG_0354.JPG/download

curl --request GET --url https://admin.typeform.com/form/aLsAcC/field/SM3co23sAfCQ/results/1a4a6ab57b29-IMG_0354.JPG/download --header 'Authorization: Bearer {MyId}'   
or   
wget https://admin.typeform.com/form/aLsAcC/field/SM3co23sAfCQ/results/1a4a6ab57b29-IMG_0354.JPG/download   

It always seems to give the same result.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="0;url=https://admin.typeform.com/login/" />

        <title>Redirecting to https://admin.typeform.com/login/</title>
    </head>
    <body>
        Redirecting to <a href="https://admin.typeform.com/login/">https://admin.typeform.com/login/</a>.
    </body>
</html>

Which is simply the login page from the site.

The weird thing is that when I use my browser itself it works just fine, so I suspect I am missing some form of authentication. It's probably a very simple problem, but Google didn't really help on that front.

So my question is, how do I download the response image. It doesn't matter if the final solution does not use curl, but a commandline tool or library is a must.

Thanks.


Solution

  • I have contacted typeform about this and they do not support this trough the api. They however told me they will send this to their product team.

    However a way around this is to login trough curl and then request the document.

    I did this with guzzle but the same applies to curl, remember that you will need to "remember" the cookies that you get which is described in Save cookies between two curl requests how to do that.

    Code examples are PHP to give you an idea, change it as fit.

    Steps to take:

    1. Do curl request to https://admin.typeform.com/login/
    2. Use the response to find the csrf_token preg_match("/loginCsrfToken\: '(.*)'/", $content, $matches);
    3. Don't forget you also need the cookies of this request
    4. Do a post to https://admin.typeform.com/login_check with the following data

      form_params' => [ '_username' => '{YOUR USERNAME}', '_password' => '{YOUR PASSWORD}', '_csrf_token' => {THE FOUND TOKEN}, 'teamsWorkspaceInvitationToken' => null, '_target_path' => 'https://admin.typeform.com/', ],

    5. If login succeeded you should be able to download your file

    UPDATE: While this works, i noticed that the login form has a recaptcha which will break this flow. But i do know that they are actually working on this at the moment. So if this stops working for you best be patient, and/or send a support ticket to typeform you need this. It might make them release this for the public sooner.