cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Dropbox download_zip api

Dropbox download_zip api

Андрей Г.2
Explorer | Level 4

I use api https://www.dropbox.com/developers/documentation/http/documentation#files-download_zip to download files from a folder, but I always get an error in call to API function "files / download_zip": HTTP header " Dropbox-API-Arg ": could not decode input as JSON
please indicate what the problem may be

5 Replies 5

Greg-DB
Dropbox Staff

The /2/files/download endpoint is a "content-download" style endpoint, so it expects the parameters (such as "path") as JSON in a 'Dropbox-API-Arg' request header. 

This "could not decode input as JSON" error indicates that the value supplied in the 'Dropbox-API-Arg' request header could not be successfully parsed, likely because it is not valid JSON.

Please check that you're supplying a valid JSON string in the 'Dropbox-API-Arg' request header. If you're not already using one, we recommend using a JSON library for reading/writing the JSON strings, instead of doing so manually.

You can also use the API v2 Explorer to prototype these calls for you. It will build the header for you, and can even show you the code for making the call.

If something still isn't working as expected, please share the relevant code and output (but please redact your access token).

Андрей Г.2
Explorer | Level 4


here is my sample code, i think here everything is right $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://content.dropboxapi.com/2/files/download_zip'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); $headers = array(); $headers[] = 'Authorization: Bearer $token'; $headers[] = 'Dropbox-Api-Arg: {\"path\": \"/test/"}'; $headers[] = "Content-Type: application/octet-stream"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) {     echo 'Error:' . curl_error($ch); } curl_close($ch);

You can also use the API v2 Explorer to prototype these calls for you. It will build the header for you, and can even show you the code for making the call.

I used this, I generated the code, but it doesn't work

Greg-DB
Dropbox Staff

Thanks for the additional information. I see you have extra \ values in your JSON string. You may have copied those from the command-line curl examples in the documentation. Those are only needed in contexts where you need to escape the subsequent " values, such as in bash, like in those examples, since the " character is already used to start the string for the header parameter for curl itself.

Since that doesn't apply to your PHP context, you shouldn't include them as they'll be sent as-is, corrupting the JSON string. Instead, you'd just want something like:

$headers[] = 'Dropbox-Api-Arg: {"path": "/test"}';

I do recommend having some other library or method build those JSON strings for you instead though.

polishchyk-a-v
Explorer | Level 3

function dropbox_download_zip($folder_path, $file_name) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://content.dropboxapi.com/2/files/download_zip'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); $headers = array(); $headers[] = "Authorization: Bearer " . DROPBOX_TOKEN . ""; $headers[] = "Dropbox-API-Arg: {\"path\": \"$folder_path\"}"; $headers[] = "Content-Type: application/octet-stream; charset=utf-8"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); curl_close($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } else { $file_name = $file_name . '.zip'; file_put_contents(get_home_path() . '/' . $file_name, $result); $zipFilePath = get_home_path() . $file_name; $zipBaseName = basename($zipFilePath); header("Content-Type: application/zip"); header("Content-Disposition: attachment; filename=$zipBaseName"); header("Content-Length: " . filesize($zipFilePath)); readfile($zipFilePath); @unlink($zipFilePath); die(); } }

here is my code, but it only works on a local server, on a real site it produces such an error, why? https://monosnap.com/file/0Dxbpzchbiq8ngZRl42SNEHqbCvmi1

Greg-DB
Dropbox Staff

I see this was also opened as a new thread, so I'll take a look and reply on that one:

https://www.dropboxforum.com/t5/Discuss-Developer-API/download-zip-api/m-p/392520#M963

Need more support?