javascript - Node 请求模块 - 发送表单数据时遇到问题

标签 javascript node.js

我正在尝试从我的 Node 服务器发送一个发布请求到 api that expects a file and other form data as an multipart/form-data .

这是我的代码的样子

var importResponse = function(csv){
  stringify(csv, function(err, output){
    request.post({
      headers: {'X-API-TOKEN':token, 'content-type' : 'multipart/form-data'},
      url: url,
      formData: {
        surveyId: surveyId,
        file: {
            value: output,
            options: {
                fileName: 'test.csv',
                contentType:'text/csv'
            }
        }
      }
    }, function(error, response, body){
      console.log(body);
    });
  });
}

使用request-debug这里是请求:

request:
   { debugId: 1,
     uri: 'https://co1.qualtrics.com/API/v3/responseimports',
     method: 'POST',
     headers:
      { 'X-API-TOKEN': 'removed',
        'content-type':
         'multipart/form-data; boundary=--------------------------010815605562947295265820',
        host: 'co1.qualtrics.com',
        'content-length': 575 } } }

和响应:

response:
   { debugId: 1,
     headers:
      { 'content-type': 'application/json',
        'content-length': '188',
        'x-edgeconnect-midmile-rtt': '28',
        'x-edgeconnect-origin-mex-latency': '56',
        date: 'Wed, 18 Jul 2018 03:57:59 GMT',
        connection: 'close',
        'set-cookie': [Array],
        'strict-transport-security': 'max-age=31536000; includeSubDomains; preload' },
     statusCode: 400,
     body:
      '{"meta":{"httpStatus":"400 - Bad Request","error":{"errorMessage":"Missing Content-Type for file part. name=file","errorCode":"MFDP_3"},"requestId":"322a16db-97f4-49e5-bf10-2ecd7665972e"}}' } }

我收到的错误是:缺少文件部分的内容类型。

我已将其添加到选项中:

options: {
    fileName: 'test.csv',
    contentType:'text/csv'
}

当我查看请求时,似乎不包含表单数据。但也许这只是 request-debug 没有显示它。

我看到了类似的SO question答案是使用 JSON.stringify。 我尝试将代码更改为以下内容:

request.post({
  headers: {'X-API-TOKEN':token, 'content-type' : 'multipart/form-data'},
  url: url,
  body: JSON.stringify({
    surveyId: surveyId,
    file: {
        value: output,
        options: {
            fileName: 'test.csv',
            contentType:'text/csv'
        }
    }
  })

但是,我收到以下错误:

{"meta":{"httpStatus":"400 - Bad Request","error":{"errorMessage":"Missing boundary header"}}}

我做错了什么?

更新 当我尝试在计算机上将文件值更改为 csv fs.createReadStream('test.csv') 时,效果很好

    file: {
        value: fs.createReadStream('test.csv'),
        options: {
            contentType: 'text/csv'
        }
    }

所以我认为我提供文件的方式有问题。我用作文件的 output 变量看起来像 "QID1,QID2\nQID1,QID2\n1,2"。我认为这是导致问题的原因,尽管该错误有点误导性。我尝试创建一个 Readable,我发现它是 StackOverFlow answer像这样:

var s = new Readable
s.push(output)
s.push(null) 

但是,这会导致输入意外结束

{"meta":{"httpStatus":"400 - Bad Request","error":{"errorMessage":"Unexpected end of input"}}}

最佳答案

我发现了这个问题。我的第一个解决方案很好,但不是 fileName 它应该是 filename

var importResponse = function(csv){
  stringify(csv, function(err, output){
    request.post({
      headers: {'X-API-TOKEN':token, 'content-type' : 'multipart/form-data'},
      url: url,
      formData: {
        surveyId: surveyId,
        file: {
            value: output,
            options: {
                filename: 'test.csv', //filename NOT fileName
                contentType:'text/csv'
            }
        }
      }
    }, function(error, response, body){
      console.log(body);
    });
  });
}

关于javascript - Node 请求模块 - 发送表单数据时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51393413/

相关文章:

javascript - 下载属性在 Firefox 中不起作用

javascript - jquery - 相当于 Prototype.js 的 "match"函数

javascript - HTML5 视频上的关闭按钮 (iPad)

javascript - 如何在更改事件上显示特定输入的 native 验证错误?

javascript - 使用 weyland 的 Durandal 优化不适用于 requirejs 路径

javascript - Express:router.post 和 router.get 之间共享逻辑

node.js - Nginx 性能问题

mysql - Heroku 部署错误 :connection refused

javascript - 未定义 forEach TypeError : undefined is not a function

node.js - 表示 : using await before bcrypt. 比较给出错误