rak007 rak007 - 1 month ago 5
reST (reStructuredText) Question

Dropbox API - Error: expected list got dict

I am currently building a routine that needs to download files from one specific Dropbox folder , send them to another server and then move them to another folder on Dropbox.

I am using the

/files/move_batch
API endpoint for Dropbox to do so.

Here are the params sent to the API to move multiples files (well I'm only trying to move one file right now as it's still not working) :

$params = array(
'headers' => array(
'method' => 'POST',
'content-type' => 'application/json; charset=utf-8',
),
'body' => json_encode(array(
'entries' => array(
'from_path' => self::$files[0],
'to_path' => '/Applications/Archives/' . substr(self::$files[0], strrpos(self::$files[0], '/') + 1),
),
'autorename' => true,
)),
);


But I keep getting the same error message :

Error in call to API function "files/move_batch": request body: entries: expected list, got dict


I don't know what the API means by a list or how it should be formated.

Answer

The entries value should be a list of dict, one per file you want to move, each one containing both a from_path and a to_path. Your code is supplying the entries value to be a single dict though. (In PHP you can make both lists and dicts using the array keyword.)

It's easier to see and work with when you break it into pieces. Here's a working sample that does that.

<?php

    $fileop1 = array(
                    'from_path' => "/test_39995261/a/1.txt",
                    'to_path'   => "/test_39995261/b/1.txt"
                );

    $fileop2 = array(
                    'from_path' => "/test_39995261/a/2.txt",
                    'to_path'   => "/test_39995261/b/2.txt"
                );

    $parameters = array(
            'entries' => array($fileop1, $fileop2),
            'autorename' => true,
    );

    $headers = array('Authorization: Bearer <ACCESS_TOKEN>',
                     'Content-Type: application/json');

    $curlOptions = array(
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => json_encode($parameters),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_VERBOSE => true
        );

    $ch = curl_init('https://api.dropboxapi.com/2/files/move_batch');
    curl_setopt_array($ch, $curlOptions);

    $response = curl_exec($ch);
    echo $response;

    curl_close($ch);

?>

To move just one file using this batch endpoint, you would change that line to something like:

            'entries' => array($fileop1),
Comments