Node.js - Send and receive Array as GET/POST using querystring
Asked Answered
B

3

15

I've got the following code, but it doesn't seem to work:

var post_req = {
    array: [
        [ {
            param1: 'something',
            param2: 123
        } ],
        [ ],
        [ ],
        [ {
            param2: 'something',
            param4: 1234,
            param1: 'hello'
        } ]
    ]
};
var data_send = querystring.stringify(post_req);

var request = client.request('POST', '/', headers);
request.end(data_send);

and

if( req.method == 'POST' ) {
    req.addListener('data', function(chunk)
    {
        POST = querystring.parse(chunk);
        console.log(POST);
    }
}

I end up with 5 sub-arrays, corresponding to the 5 parameters in the objects but with extra '][' characters in their names:

{ array: 
   [ { '][param1': 'something' }
   , { '][param2': '123' }
   , { '][param2': 'something' }
   , { '][param4': '1234' }
   , { '][param1': 'hello' }
   ]
}
Babs answered 3/1, 2011 at 17:30 Comment(1)
Looks like node's querystring module wasn't designed to handle nested arrays (possibly empty ones). If it's a POST request you could use JSON.stringify and JSON.parse instead?Unciform
A
13

There is a new node package that fixes this: "npm install qs".

https://github.com/ljharb/qs

"query string parser for node supporting nesting, as it was removed from 0.3.x, so this library provides the previous and commonly desired behaviour (and twice as fast)"

If anyone can tell me why it was removed from 0.3.x, I will give you an upvote for your comment. (I want my confidence in Node.js restored.)

Awaken answered 25/11, 2011 at 18:54 Comment(0)
U
6

To confirm my comment above, node's querystring.stringify function won't handle nested arrays (at the time of writing).

You can see the source of stringify at https://github.com/ry/node/blob/master/lib/querystring.js

Note that it handles one level of arrays but it doesn't recurse. When it finds an array it uses stringifyPrimitive to encode the array's values. You can see that stringifyPrimitive doesn't handle arrays, only number, boolean and string.

As I suggested in my comment, given that you're using a POST request a better idea would be to use JSON encoding for your complex data structure.

Or use https://github.com/visionmedia/node-querystring as suggested by @FriendlyDev

Unciform answered 3/1, 2011 at 22:35 Comment(1)
Just in case someone searches for this.. I ran into the same problem, with regard to handling of one level of arrays but it doesn't recurse. The way I got around it was to create the object in javascript, json stringify the object and attached that string to a singular query param i.e. ?array=jsonString. Then in the code behind (node.js & express), I pickup the singular param with req.query.array and use JSON.parse to get back the obj, i.e. array = JSON.parse(req.query.array); linkDeemster
R
6

Don't use querystring.parse for recreating a JSON object sent as string. Use instead JSON.parse. And use JSON.stringify instead of querystring.stringify

querystring is useful when you send parameter encoded in the url or when you post a form. But there is no point in using it if you send just one JSON object with all your data.

In your scenario I would dismiss the querystring library and use JSON library, which is already included. It would be cleaner and faster.

http://www.json.org/js.html

Ruelas answered 2/3, 2015 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.