cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
We just wanted to say thank you! Check out our customer appreciation video here.

Discuss Dropbox Developer & API

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

Re: JSON returning NULL from Uploaded file

JSON returning NULL from Uploaded file

BMKN
Explorer | Level 3

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 3

Greg-DB
Dropbox Staff

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
Explorer | Level 3

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
Dropbox Staff

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.

Need more support?