In node.js, I'm using code from here to extract POST data.
that is:
var qs = require('querystring');
function (request, response) {
if (request.method == 'POST') {
var body = '';
request.on('data', function (data) {
body += data;
request.connection.destroy();
});
request.on('end', function () {
var post = qs.parse(body);
console.log(post.foo);
});
}
}
I am making requests using the Python requests library. When the array foo
has more than one element the output is as expected.
requests.post(url, data={'foo': ['bar', 'baz']})
gives ['bar', 'baz']
However if the array has only one element in it the variable foo
becomes a string!
requests.post(url, data={'foo': ['bar']})
gives bar
and not ['bar']
.
I would like to not do something like:
if (typeof post.foo === 'string')
post.foo = [post.foo]
to ensure that the client sends only arrays.