Want to know more about Dash? Check out how Amy uses Dropbox and Dash to make her day easier here! 

Forum Discussion

BMKN's avatar
BMKN
Explorer | Level 3
6 years ago

JSON returning NULL from Uploaded file

I hope you can help. I am trying to get a URL from DropBox using cURL. I am able to upload the file with cURL and retrieve the uploaded file. I have taken the uploaded ID and passed it into the Headers and I get back NULL after I decoded JSON. I am not sure what I am missing. I have attached the code below.

 


$cheaders = array('Authorization: Bearer xxx',
'Content-Type: application/octet-stream',
"Dropbox-API-Arg: {\"path\": \"/Homework/math/Prime_Numbers.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": true}");

//var_dump($cheaders);

$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo '<pre>';

var_dump(json_decode($response));

$obj =json_decode($response);
print str_replace('id:','',$obj->{'id'});

echo '</pre>';
fclose($fp);

$File_ID = $obj->{'id'};


$cheaders2 = array('Authorization: Bearer xxx',
"Content-Type: application/json" ,
"{\"file\": \"id:zFe2SHxKbHEAAAAAAAJc-g\",\"actions\": []}"

);

 

$ch2 = curl_init('https://api.dropboxapi.com/2/sharing/get_file_metadata');
curl_setopt($ch2, CURLOPT_HTTPHEADER, $cheaders2);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
$response2 = curl_exec($ch2);

var_dump(json_decode($response2));

3 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    6 years ago

    What's the value of $response2 itself, before you try to json_decode it? It's not guarantee to be JSON, e.g., if the called failed. If the call failed, the response body would contain an error. The error could be plain text, not JSON.

     

    You should always check the status code to see if the call succeeded or failed, and the 'Content-Type' header of the response to see what type the response is before attempting to parse it.

  • BMKN's avatar
    BMKN
    Explorer | Level 3
    6 years ago

    Hi Greg,

     

    I get the following error  string(103) "Error in call to API function "sharing/get_file_metadata": request body: could not decode input as JSON"

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    6 years ago

    Thanks for sharing that. That is the error message from the Dropbox API. (Since it's an error about the malformed request, it is just text and not JSON, and so json_decode can't read it. You can check the response's 'Content-Type' to check this programmatically.)

     

    Anyway, the error message is indicating that the call failed because you did not send valid JSON containing the API call parameters in the request body. The /2/sharing/get_file_metadata endpoint is an "RPC" style endpoint, meaning that it expects the parameters as JSON in the request body. (This is different than /2/files/upload, which, as a "content-upload" style endpoint, takes its parameters as JSON in a 'Dropbox-API-Arg' request header.)

     

    Looking at your code, I do see that you're attempting to send the /2/sharing/get_file_metadata parameters ('file' and 'actions') in a header, which is incorrect. You'll need to update your code for that call to send them in the request body instead.

About Discuss Dropbox Developer & API

Node avatar for Discuss Dropbox Developer & API
Make connections with other developers813 PostsLatest Activity: 2 days ago
266 Following

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!