【问题标题】:How do I encode an array in a form field in NodeJS' request?如何在 NodeJS 的请求中对表单字段中的数组进行编码?
【发布时间】:2015-06-14 09:52:44
【问题描述】:

我使用的 API 要求我将数组放入表单字段中。给出的示例是用 PHP 编写的,但我使用的是 NodeJS。 API 期望其中一个字段是一个数组,我很难弄清楚如何做到这一点。

PHP 示例如下所示:

$request->buildPostBody(array(
 'reference' => array(
 'line1' => 'Soundboard Setup',
 'line2' => 'Thank you for the order',
 'line3' => 'Our reference is: 3993029/11BD'
 ),
 'lines' => array(
 array('amount' => 50,
 'amount_desc' => 'panels',
 'description' => 'Sound buttons',
 'tax_rate' => 21,
 'price' => 5.952
 ),
 array('amount' => 1,
 'amount_desc' => '',
 'description' => 'Wooden case',
 'tax_rate' => 21,
 'price' => 249
 ),
 array('amount' => 10,
 'amount_desc' => 'hours',
 'description' => 'Support',
 'tax_rate' => 6,
 'price' => 62.5
 ),
 array('description' => 'This is a textline'))
));

在 NodeJS 中,我尝试过这个(除其他外):

var formData = {
  reference: [{'line1':'something'}], // <- this isn't going to fly
  lines: [{
    'amount': amount,
    'amount_desc': 'amount_desc',
    'description': 'description',
    'tax_rate': 0,
    'price': 100
  }]
};

request.post({
    url: 'https://www.factuursturen.nl/api/v1/invoices/',
    formData: formData,
    headers: {
      'Authorization': 'Basic ' + new Buffer(user + ':' + key).toString('base64')
    }
  }, function (error, response, body) {
    if (!error && [200, 201, 204].indexOf(response.statusCode) > 0) {
      console.log('posted ': ', response.statusCode);
    } else {
      console.log('problem with request: ', error, response.statusCode, body);
    }
  }
);

当我尝试像这样传递一个对象时:reference: {'line1':'foo'} 我从节点收到一个错误:

node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33
  source.on('error', function() {});
         ^
TypeError: Object #<Object> has no method 'on'

我最好如何将这个圆钉插入那个方孔?

【问题讨论】:

    标签: php arrays node.js form-data node-request


    【解决方案1】:

    在 PHP 中数组可以是数组它们可以是关联数组,它们本质上是 JavaScript 中的对象。

    array() 指定

    可以使用 array() 语言结构创建array。它接受任意数量的逗号分隔键 => 值对作为参数。

    array(
        key  => value,
        key2 => value2,
        key3 => value3,
        ...
    )
    

    在 JavaScript 中,上述内容本质上只是一个对象

    {
        key: 'value',
        key2: 'value2'
        key3: 'value3'
        ...
    }
    

    它不会是包裹在数组[{…}] 中的对象,这就是你对reference 所做的事情

    所以要模仿 PHP 结构,您的数据应该是这样的:

    var formData = {
      reference: {'line1':'something'}, // <- this should work now
      lines: [{  // <- this is still an array of objects [{}], as in PHP too, it's a nested array
        amount: amount,
        'amount_desc': 'amount_desc',
        'description': 'description',
        'tax_rate': 0,
        'price': 100
      }]
    };
    

    但正如您所注意到的,这会导致另一个问题。


    当我尝试像这样传递一个对象时:reference: {'line1':'foo'} 我收到来自 Node 的错误:

    node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33
      source.on('error', function() {});
             ^
    TypeError: Object #<Object> has no method 'on'
    

    这个错误在这里解释:https://github.com/request/request/issues/1495 基本上form-data 只需要简单的对象,可能只有 1 级深。

    试试这个:

    var formData = {
      'reference.line1': 'something',
      'lines[0].amount': amount,
      'lines[0].amount_desc': 'amount_desc',
      'lines[0].description': 'description',
      'lines[0].tax_rate': 0,
      'lines[0].price': 100,
    };
    

    【讨论】:

    • 有趣的是,如果我尝试从节点收到一个奇怪的错误:node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33 source.on('错误',函数(){}); ^ TypeError: Object # has no method 'on'
    • 感谢您阐明 PHP 数组映射到 js 对象的方式 @laggingreflex。我想我现在只需要弄清楚如何将数组或对象编码到nodejs中的formdata字段中
    • 您的更新是正确的解决方案。我平行地得出了相同的结果,只是来这里回答我自己的问题,看到你也到了那里。我认为你应该删除“所以你的数据应该是这样的”部分。
    猜你喜欢
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2016-05-01
    • 2011-01-15
    • 2015-12-22
    相关资源
    最近更新 更多