如何使用请求替换文件选择器文件?

how to replace a filepicker file using request?

我有一个 pdf 文件的文件选择器 url。我需要用另一个 pdf 文件替换那个 pdf 文件,同时在 nodejs 中保持相同的 url。

我有一个 base64 格式的 pdf 字符串,我想将它发送到 filepicker。我知道我必须执行某种类型的 post 请求(最好使用请求模块),但不确定如何执行。

提前致谢!

更新

以下是请求,但 pdf 在文件选择器中被保存为损坏的文件。我认为我没有为 base64 pdf 正确设置请求对象。我从 pdfunite 获取字符串,它工作正常,因为我尝试将它作为附件通过电子邮件发送并且它正确显示。

    request.post({url: 'https://www.hypdf.com/pdfunite', encoding: null, formData: formData}, function(err, res3, body3){
        console.log(res3.statusCode);
        console.log(err);
        dpdf = body3.toString('base64');  //this is the pdf I want to send to filepicker

        var options = {
        method: 'POST',
        body: dpdf,
        url: FILEPICKER_URL,
        headers: [
          {
            name: 'content-type',
            value: 'application/pdf'
          }
        ]
      };

      request(options, function(err, httpResponse, body){ 
        console.log('body: ', body);
        console.log('code ', httpResponse.statusCode)
      });
});

要覆盖现有的 filepicker 文件,请发送 POST 带有文件 url 和数据负载的请求。

这是文本文件的基本 node.js 示例,pdf 应该类似。

var request = require('request');

var existedFileUrl = 'https://www.filepicker.io/api/file/9jUqfXmSoWZiTN4Zcq9H';

request(existedFileUrl, function (error, response, body) {

    console.log('Original content: ', body);

    var input = process.argv[2] || 'new_text';

    var options = {
      method: 'post',
      body: input,
      // append base64decode option for base64 string
      url: existedFileUrl + '?base64decode=true'
    };

    request(options, function(err,httpResponse,body){ 
        console.log('body: ', body);
    });
});

另请查看文档 https://www.filepicker.com/documentation/file_ingestion/rest_api/writing