We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here. 

Forum Discussion

benjaminkromer's avatar
benjaminkromer
Explorer | Level 4
3 years ago

Try to Dowload files getting Error 400

I want to Download a file with a API Call. First I thought it would be a good idea to download it from the sharelink by changing the query parameter ?dl to 1 and then use axios to just get that and save the file buffer. This worked for the most files, but sometimes the downloaded PDFs where broken and could'nt be opened.

So I tried the download endpoint "https://content.dropboxapi.com/2/files/download" without the SDK.

 

 

const axios = require('axios');
let config = {
  'method': 'POST',
  'url': 'https://content.dropboxapi.com/2/files/download',
  'headers': {
    'Dropbox-API-Select-User': 'MYAPISELECTUSER',
    'Dropbox-API-Path-Root': '{".tag": "namespace_id", "namespace_id": "MYNAMESPACE"}',
    'Dropbox-API-Arg': '{"path":"/MY Folder/myfile.PDF"}',
    'Authorization': 'Bearer MYBEARERTOKEN'
  }
};
axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

 

 

I get the error: "Response failed with a 400 code"

When I try this in Postman it works but the same code on my nodejs server gives me status 400 codes. No further explanations error messages nothing.

 

Then I tried with the Dropbox SDK and 'isomorphic-fetch' instead of axios.

 

 

const fs = require("fs");
var Dropbox = require('dropbox').Dropbox;
var fetch = require('isomorphic-fetch');

const dlFile = async () => {
  try {
    var dbx = new Dropbox({ accessToken: 'MYBEARERTOKEN', fetch: fetch });
    const response = await dbx.filesDownload( {path: '/My Folder/myfilename.PDF' })
    console.log(JSON.stringify(response));
    fs.createWriteStream("test.pdf",response.data); 
  } catch (err) {
    console.log(`${err} ${err.message}`);
    return err;
  }
};
const main = async function () {
  await dlFile();
};

main();

 

 

Same result Status code 400.

Finally I also tried the request module

 

 

const fs = require("fs");
const request = require('request')

const dlFile = async () => {
  try {
    var options = {
      'method': 'POST',
      'url': 'https://content.dropboxapi.com/2/files/download',
      'headers': {
        'Dropbox-API-Select-User': 'MYSELECTUSERID',
        'Dropbox-API-Path-Root': '{".tag": "namespace_id", "namespace_id": "MYNAMESPACE"}',
        'Dropbox-API-Arg': '{"path":"/My Folder/myfilename.PDF"}',
        'Authorization': 'Bearer MYTOKEN'
      }
    };
    request(options, function (error, response) {
      if (error) throw new Error(error);
      //console.log(response)
      
      fs.writeFileSync("myfilename.pdf", response.body); 
    });
  } catch (err) {
    console.log(`${err} ${err.message}`);
    return err;
  }
};
const main = async function () {
  await dlFile();
};

main();

 

 

The buffer from response.body looks promising: 

"%PDF-1.4
%
1 0 obj
<<
/SM 0.001
/Type/ExtGState
>>
endobj
2 0 obj
<<
/Width 1521
/ColorSpace/DeviceCMYK
/Height 357
/Subtype/Image
/Type/XObject
/Length 2171988
/BitsPerComponent 8
>>
stream
and so on...."

 

 

But when I open the PDF file it has a frame with the right format but it's empty just white.

What am I missing whats the recommended way?

Why is it working in Postman but not on my NodeJs server?

 

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    It looks like the different clients are producing different requests. For the requests that are failing with a 400 status code, print out the raw response body. The response body should contain a more useful error message indicating what the issue is.

     

    For the call where you are getting that "%PDF-1.4..." data back, that indicates that the call succeeded, and Dropbox is returning the requested file data. The /2/files/download endpoint returns the exact bytes that are stored on the Dropbox server, which in this case is for a PDF file. From there, you can do whatever you need to with the data, such as passing it to some PDF viewer. I can't offer help with using the PDF data/format itself though as that's outside the scope of Dropbox API support. You may need to check the documentation for whatever PDF viewer you're using, or make sure the file originally uploaded to Dropbox is the version of the PDF you're looking for.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,876 PostsLatest Activity: 5 hours ago
325 Following

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!