Like POST multiple values ​​with the same name in Node.js Request

I need to programmatically send multiple values ​​to POST (in this example, in the US) using node.js and Request.

For example, an HTML form might be

<select name="stateprov[]" id="stateprov" multiple="multiple" >

and then 50 options ..., one for each state

And the submitted form data will look like

stateprov%5B%5D=CA&stateprov%5B%5D=WI

How to do this using a query? Given that I have an array of states, ['CA', 'WI'}, I tried

form['stateprov[]'] = states  
   fails 
it generates stateprov%5B%5D[0]=WI&stateprov%5B%5D[1]=CA as the output

form ['stateprov []'] = states.join (',') does not work either

BTW, Node people, I really like the project, there are a lot of cool things, but your documentation is less large.

Followup: , , Request (https://npmjs.org/package/request) qs (https://npmjs.org/package/qs), , [0] [1]. Node queryString (http://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq) .

Followup # 2: Mikeal Rogers, , , . , http.

+3
3

, restler. , npm 'request' Just do

import request from 'request';
let data = { subject: 'a message', recipients:['person1@gmail.com', 'person2@gmail.com'] }
// define your data above. I was having issues with the recipients needing to repeat
let options = {
  form: data, qsStringifyOptions: {arrayFormat: 'repeat'}
}
request.post(url, options, function(err, res, body){
   //callback. note request sends 3 params to callback 
})

Q, promises. . , , , , soemone, .

+8

, , , ...

, , . npm qs (https://npmjs.org/package/qs) , , "" '[n]'.

function stringifyArray(arr, prefix) {
  var ret = [];
  if (!prefix) throw new TypeError('stringify expects an object');
  for (var i = 0; i < arr.length; i++) {
    ret.push(stringify(arr[i], prefix + '[' + i + ']'));  <<< see here
  }
  return ret.join('&');
}

, :

foo[0]=value0&foo[1]=value1

, , , , , , , HTML-. HTML , : -)

, node, querystring.stringify, , ,

foo=value0&foo=value1

- Request.form() ( 974)

this.body = *querystring*.stringify(form).toString('utf8')

, , , . . "" . , - ( "" ), index.js, factory request(). "" new request.js. : require('request/request.js')

: ( https://gist.github.com/MorganConrad/8827916)

var Request = require('request/request.js');  // IMPORTANT - specify request.js, don't get index.js!!!
var querystring = require('querystring');

MyRequest.prototype = Object.create(Request.prototype);
MyRequest.prototype.constructor = MyRequest;

function MyRequest(options, callbackfn) {
  "use strict";
  if (callbackfn)
    options.callback = callbackfn;
  options.method = options.method || 'POST';
  Request.prototype.constructor.call(this, options);
}

MyRequest.prototype.form = function (form) {
  "use strict";
  if (form) {
    this.setHeader('content-type', 'application/x-www-form-urlencoded; charset=utf-8');
    this.body = querystring.stringify(form).toString('utf8');
    return this;
  }

  else
    return Request.prototype.form.apply(this, arguments);
};


module.exports = MyRequest;
0
var querystring = require('querystring');
var form = {
//form parameters
}
request.post(http + querystring.stringify(form), callback);

Declare the form object outside the request. Then call querystring.stringify (form), which returns the desired string, and just add that string to the url. URL encoding in this manual mode avoids child classes and update problems.

-1
source

All Articles