I am using dropbox
for node
: "dropbox": "^4.0.17"
and trying to upload a file.
Here is the example code:
require('dotenv').config();
const fs = require('fs');
const fetch = require('isomorphic-fetch');
const Dropbox = require('dropbox').Dropbox;
const config = { accessToken: process.env.DROPBOX_ACCESS_TOKEN, fetch: fetch };
const dbx = new Dropbox(config);
const fileContent = fs.readFileSync('full path to some pdf files');
dbx.filesUpload(fileContent)
.then((response) => {
console.log('response', response);
})
.catch((err) => {
console.log('error', err);
});
and here is the response:
{ error: '<html>\r\n<head><title>400 Request Header Or Cookie Too Large</title></head>\r\n<body>\r\n<center><h1>400 Bad Request</h1></center>\r\n<center>Request Header Or Cookie Too Large</center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n',
response:
Body {
url: 'https://content.dropboxapi.com/2/files/upload',
status: 400,
statusText: 'Bad Request',
headers: Headers { _headers: [Object] },
ok: false,
body:
PassThrough {
_readableState: [ReadableState],
readable: false,
domain: null,
_events: [Object],
_eventsCount: 4,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: true,
_transformState: [Object] },
bodyUsed: true,
size: 0,
timeout: 0,
_raw:
[ <Buffer 3c 68 74 6d 6c 3e 0d 0a 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e 34 30 30 20 52 65 71 75 65 73 74 20 48 65 61 64 65 72 20 4f 72 20 43 6f 6f 6b 69 65 20 ... > ],
_abort: false,
_bytes: 226 },
status: 400 }
The argument passed to filesUpload
should be a FilesCommitInfo
, not just the file contents directly. You can find an example of what it should look like here.
So, for your code, instead of:
dbx.filesUpload(fileContent)
you should so something like:
dbx.filesUpload({ path: '/some/destination/file/path/and/name.ext', contents: fileContent})
(The way you currently have it will end up trying to send the entire file contents as the API call parameters, which happen to be sent in a header, causing the error you get.)