如何使用node.js中的请求以多部分形式发送对象 [英] How to send an object in multipart formData using request in node.js

查看:203
本文介绍了如何使用node.js中的请求以多部分形式发送对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用请求来制定POST,但我不断收到每当我尝试将to对象添加到formData时,都会出现错误.

I'm trying to formulate a POST using request, but I keep getting an error anytime I try and add the to object to formData.

var fs      = require('fs');
var request = require('request');
var file    = './test/assets/test.pdf';

var opts = {
  url: 'my_service',
  method: 'POST',
  auth: { user: 'username', password: 'password' },
  json: true,
  formData: {
    front: fs.createReadStream(file),
    to: {
      name: 'joe bob',
      address_1: '123 main st',
      ...
    }
  }
};

request(opts, function(err, resp, body) {
  console.log(err, body);
});

这是错误:

/sandbox/project/node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33
  source.on('error', function() {});
         ^
TypeError: undefined is not a function
    at Function.DelayedStream.create (/Users/me/sandbox/project/node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
    at FormData.CombinedStream.append (/Users/me/sandbox/project/node_modules/request/node_modules/combined-stream/lib/combined_stream.js:43:37)
    at FormData.append (/Users/me/sandbox/lproject/node_modules/request/node_modules/form-data/lib/form_data.js:43:3)
    at appendFormValue (/Users/me/sandbox/project/node_modules/request/request.js:466:21)
    at Request.init (/Users/me/sandbox/project/node_modules/request/request.js:477:11)
    at new Request (/Users/me/sandbox/project/node_modules/request/request.js:264:8)
    at request (/Users/me/sandbox/project/node_modules/request/index.js:50:10)
    at Object.<anonymous> (/Users/me/sandbox/project/test.js:30:1)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)

如果删除to对象,则一切正常.

If I remove the to object, everything works.

这是为什么-我在做什么错了?

Why is this - what am I doing wrong?

推荐答案

formData属性不能处理作为值传入的对象.请参阅文档.一种解决方法是使用JSON.stringify

The formData attribute doesn't handle objects passed in as a value. see the documentation. A solution would be to use JSON.stringify

var fs      = require('fs');
var request = require('request');
var file    = './test/assets/test.pdf';

var toObj = {
  name: 'joe bob',
  address_1: '123 main st',
  ...
};
var opts = {
  url: 'my_service',
  method: 'POST',
  auth: { user: 'username', password: 'password' },
  json: true,
  formData: {
    front: fs.createReadStream(file),
    to: JSON.stringify(toObj)
  }
};

request(opts, function(err, resp, body) {
  console.log(err, body);
});

注意:实际上,它是仅支持字符串的表单数据包.请求使用表单数据.这是他们的使用文档,其中提到使用字符串,缓冲区和文件流."

note: It's actually the form-data package which supports only strings. Request uses form-data. Here's their usage doc which mentions using "a string, a buffer and a file stream."

这篇关于如何使用node.js中的请求以多部分形式发送对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆