php - How do I encode an array in a form field in NodeJS' request? -
i'm using api requires me put array in form field. example given in php, i'm using nodejs. api expects 1 of fields array, , i'm having hard time figuring out how this.
the php example looks this:
$request->buildpostbody(array( 'reference' => array( 'line1' => 'soundboard setup', 'line2' => 'thank 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 textline')) ));
in nodejs i've tried (among other things):
var formdata = { reference: [{'line1':'something'}], // <- isn't going 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 request: ', error, response.statuscode, body); } } );
when try pass object so: reference: {'line1':'foo'}
error 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'
how best go shoehorning round peg square hole?
in php arrays can arrays and can associative arrays, objects in javascript.
specifying array()
an array can created using array() language construct. takes number of comma-separated key => value pairs arguments.
array( key => value, key2 => value2, key3 => value3, ... )
in javascript above just object
{ key: 'value', key2: 'value2' key3: 'value3' ... }
it not object wrapped inside array [{…}]
, you're doing reference
so mimic php structures data should this:
var formdata = { reference: {'line1':'something'}, // <- should work lines: [{ // <- still array of objects [{}], in php too, it's nested array amount: amount, 'amount_desc': 'amount_desc', 'description': 'description', 'tax_rate': 0, 'price': 100 }] };
but causes problem have noticed.
when try pass object so:
reference: {'line1':'foo'}
error 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'
this error explain here: https://github.com/request/request/issues/1495 form-data
takes simple object 1 level deep.
try this:
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, };
Comments
Post a Comment