How to download submission attachments from the REST API

Hello,

I’m working on an app that should receive submissions throught the REST API of Kobotoolbox.
I receive submissions correctly, but when i try to download attachments for instance by using the provided attachment url download_url property; the HTTP request fails with a 403 Forbidden error.

I think it has to do with authentication because 403 is related to the lake of valid credentials, also when i go in the survey settings and activate “public access” to the form and submissions; i no longer get any auth error, i can download the attachment with no issue.

My question is, how can i keep the security settings activated while being able to download the attachments from my app. Should i pass credentials through the headers of the request? i tried with my login and password using Authorization: Basic …base64-encoded-creds but i still get 403. Perhaps i’m missing something.

FYI: My app is developped in PHP, actually i use the copy() function of PHP to download the attachment.

Any help is welcome, Thank you

With further trial, i can now download the attachment using copy(), here is a sample code:

<?php


$source = "https://kc.kobotoolbox.org/media/small?media_file=youssouf_traore%2Fattachments%2Faeacc024$
$target = "topo.jpg"; //File destination name
file_put_contents($target, ""); //We have to create the destination file prior to using copy().

 //Options on the request:
 $options = array(
"http" => array(
    "method"=> "GET",
    "header" => "Authorization: Basic "  . base64_encode("youssouf_traore:my-secret-password")
    )
 );
$streamContext = stream_context_create($options);
copy($source, $target, $streamContext);

?>
2 Likes

Hi @youssouf,

Thank you for sharing this excellent workaround to the entire community! It should help a lot of users. Expecting the same in the upcoming days!

Have a great day!