node.js - 如何修复 Dropbox Node API 错误 400 请求 header 或 Cookie 太大

标签 node.js dropbox dropbox-api dropbox-sdk

我正在为 node 使用 dropbox:"dropbox": "^4.0.17" 并尝试上传文件。 这是示例代码:

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);
    });

这是回应:

{ 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 }

最佳答案

传递给 filesUpload 的参数应该是 FilesCommitInfo ,而不仅仅是直接的文件内容。你可以找到an example of what it should look like here .

因此,对于您的代码,而不是:

dbx.filesUpload(文件内容)

你应该这样:

dbx.filesUpload({ path: '/some/destination/file/path/and/name.ext', contents: fileContent})

(您目前的方式最终会尝试将整个文件内容作为 API 调用参数发送,而这些参数恰好在 header 中发送,从而导致您收到错误。)

关于node.js - 如何修复 Dropbox Node API 错误 400 请求 header 或 Cookie 太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55494501/

相关文章:

ios - 使用ObjectiveDropboxOfficial框架获取用户信息

php - 在 Dropbox API 中获取带有元数据的下载链接

ios - 适用于 iOS 的 Dropbox 集成

javascript - 如何使用位置插入 MongoDB 集合

java - Dropbox API v2 中的全局光标

ios - 无法使用 Dropbox API for Swift 创建共享链接

ios - NSInputStream 与 url 在 iOS 中出现 nil

node.js - Sequelize 转换

Javascript 新日期输出错误的时间

javascript - 使用 requirejs 优化器优化后,Angularjs 找不到模块