How to POST multiple values same name in Node.js Request
forms, javascript, node.js, post, request
Solution
Stumbled on this issue and never got it to work using restler. I did find that it works using the npm module '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
})
I wrapped this inside of the Q library to make promises. Worked out well. A bit of a pain because I needed to switch libraries, but hopefully this helps soemone who stumbles on this later.
Problem
I need to programmatically submit multiple values to a POST (in this example, US states), using node.js and Request. For example, the HTML form might be be ``` <select name="stateprov[]" id="stateprov" multiple="multiple" > ``` followed by 50 options..., one per state And the submitted form data would look like ``` stateprov%5B%5D=CA&stateprov%5B%5D=WI ``` How can I do this using Request? Given that I have an array of states, ['CA','WI'}, I've tried ``` form['stateprov[]'] = states fails it generates stateprov%5B%5D[0]=WI&stateprov%5B%5D[1]=CA as the output ``` form['stateprov[]'] = states.join(',') doesn't work either BTW, Node people, I'm really trying to like the project, there's a lot of cool things, but your documentation is less than great. Followup: I think the problem might be that Request (https://npmjs.org/package/request) uses qs (https://npmjs.org/package/qs) to encode the form data, and it adds the extraneous [0] and [1]. Node's built in queryString (http://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq) does the encoding that I want. Followup #2: Chatted with Mikeal Rogers who does a great job supporting Request, and he basically said that I can't do it this way in Request. Since I'm not exploiting many of the cool features of Request I'll look at the more basic http.