cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Back again with another edition of 'How we use Dropbox', so find out how Emma uses to-do lists to get it all done 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: 

Dropbox API v2 - Empty file on upload

Dropbox API v2 - Empty file on upload

Ismaelw
New member | Level 2

Hi there

I use the dropbox v2 api to upload a file using laravel and guzzle.

However the file in dropbox shows empty. Those files have a sie of 0 bytes.

It have to do with my post call to the API because the file contents are still there before calling the upload call.

 

$client = new Client;

            $response = $client->post("https://content.dropboxapi.com/2/files/upload", [
                'headers' => [
                    'Authorization' => 'Bearer '.$this->getAccessToken(),
                    'Dropbox-API-Arg' => json_encode([
                        'path' => $path,
                        'mode' => 'add',
                        'autorename' => true,
                        'mute' => true,
                        'strict_conflict' => false
                    ]),
                    'Content-Type' => 'application/octet-stream',
                    'data-binary' => "@$file"
                ]
            ]);

            return json_decode($response->getBody()->getContents(), true);

 

UPDATE

After some testing I made it work with using

'body' => fopen($file, "r")

instead of 

'data-binary' => "@$file"

as described in the dropbox api v2 documentation.

Is there a better solution to this?

1 Accepted Solution

Accepted Solutions

Re: Dropbox API v2 - Empty file on upload

Greg-DB
Dropboxer

[Cross-linking for reference: https://stackoverflow.com/questions/59904691/laravel-dropbox-api-v2-empty-file-on-upload ]

The Dropbox API /2/files/upload endpoint is a "content-upload" style endpoint, meaning that it expects the file data for the upload in the HTTPS request body. Exactly how you set the file data in the request body will depend on what HTTPS client you're using. The example included with the documentation is written for curl on the command line, which uses curl's "data-binary" parameter to set the request body. 

Other HTTPS clients, such as Guzzle in your case, may use different option names. It sounds like you've already figured out the equivalent for Guzzle and got this working. I can't speak to whether or not there's a better option or method in Guzzle though, as that's made by a third party.

View solution in original post

1 Reply 1

Re: Dropbox API v2 - Empty file on upload

Greg-DB
Dropboxer

[Cross-linking for reference: https://stackoverflow.com/questions/59904691/laravel-dropbox-api-v2-empty-file-on-upload ]

The Dropbox API /2/files/upload endpoint is a "content-upload" style endpoint, meaning that it expects the file data for the upload in the HTTPS request body. Exactly how you set the file data in the request body will depend on what HTTPS client you're using. The example included with the documentation is written for curl on the command line, which uses curl's "data-binary" parameter to set the request body. 

Other HTTPS clients, such as Guzzle in your case, may use different option names. It sounds like you've already figured out the equivalent for Guzzle and got this working. I can't speak to whether or not there's a better option or method in Guzzle though, as that's made by a third party.

View solution in original post

Poll
We love to learn from the educators who use Dropbox. Whether you teach kids, teens, adults or a combination of all three, we want to know what apps and integrations you use with Dropbox to help with teaching. Which of the ones below is your favorite, or most used tool?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?