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

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' } ] }

-------------Problems Reply------------

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

https://github.com/visionmedia/node-querystring

"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.)

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

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

Category:node.js Views:1 Time:2011-01-03

Related post

  • Obj-C send and receive array over tcp sockets for iphone. 2012-03-10

    I am trying to figure to figure out how and if you can send and recieve arrays over TCP socket. I am a bit of a newbie at objective C but I have been able to send and receive strings. I just want to get it to do arrays now. (void)stream:(NSStream *)t

  • Sending and receiving arrays over COM 2012-02-10

    What is the right way to receive and send arrays over COM? Here's my attempt so far: a safearray of doubles wrapped in a variant. //takes variant holding safearray of doubles //returns a similar variant having multipled every element by 2 STDMETHODIM

  • How to combine subarrays of different widths using only one array for send and receive in MPI 2012-02-29

    I am having a hell of a time with this problem: Using MPI, I want to combine several contiguous, non-overlapping columnar chunks of a 2-dimensional array distributed in several MPI processes into one array residing at the root process. The main condi

  • How to send and receive an array of character pointers char *argv[] from server to client in linux socket programming 2010-12-01

    I have a char * array like char *options[n] ; // n = 2 just for example . options[0] = "How to"; options[1] = "Send"; How do I send "options" from server to client , using just one function call like send. Since the prototype of send is like this int

  • How to UDP send and receive on same port? 2009-10-28

    I need to be able to send and receive UDP packets on the same port. I am able to listen, on say port 5000, but my send uses a random high port. The system I am working written in VB with does this and my need is to write a UDP responder for debugging

  • Dilemma of Sending a Byte Array from JavaScript to COM 2010-03-14

    I'm having a bit of a problem because neither Javascript nor ActiveX (written in C++) are behaving like good little children. All I'm asking them to do is for Javascript to send a byte array and for the ActiveX to receive the byte array correctly in

  • Priority send and receive 2010-05-19

    I have a problem that I wonder if it can be efficiently implemented in ERLANG. I have a bunch of nodes communicating with each other using a protocol that adds priority information to messages. I would like to be able to send the highest priority mes

  • Sending 2 dim array using scatter 2010-05-19

    I am a beginner in MPI, and i am using C Language, and Simulator for Processors (MPICH2), i wrote the following code to send a 2D array to make 2 processors take a line from it but it produces error when running MPICH2, the code is: #include <stdi

  • Sending a byte array from PHP to WCF 2010-06-06

    I have to send a byte array (encoded photo) from my PHP client to the WCF host. when I do a var_dump() on my array in PHP I get an array[2839] which is ok but on the server side when I debug I see that received array is only byte[5]... Any idea how I

  • WCF Webservice for sending and receiving larget byte []'s 2010-09-17

    I've read numerous posts about this and have received conflicting info - I'm trying to 'boil down' a number of questions others have asked in hopes a good clear answer can exist somewhere on the web :-) I am writing .NET application for my PC that co

  • Problem with Sending and Receiving Files with SPP over Bluetooth 2010-11-28

    I am attempting to transfer files (MP3s about six megabytes in size) between two PCs using SPP over Bluetooth (in Java, with the BlueCove API). I can get the file transfer working fine in one direction (for instance, one file from the client to the s

  • MPI send and receive struct of structs 2011-02-26

    how can i send and receive this type of structs via MPI? struct controlPoint{ int hour,minute,second,x,y,z; }; struct flight{ int flightNum, controlNum; vector<controlPoint> point; }; vector<flight> flights; a sample code will be very hel

  • Sending a byte array with Java socket 2011-04-18

    I am programming an application which allows to download pdf files from a server. In my server side, I get a bytebuffer of my pdf file thanks to the pdfview library. I fill byte arrays with my byte buffer and then I send the byte arrays with a DataOu

  • Send and receive binary data over web sockets in Javascript? 2011-04-23

    It is possible to send and receive binary data over web sockets in Javascript? Could I, for example, implement an SSH client using web sockets? --------------Solutions------------- The next draft (hybi-07) of the WebSockets specification is being imp

  • sending multiple byte array over the socket 2011-04-29

    I want to send multiple byte array from the client and server ? I was able to send/Receive one byte array from client and send / Receive one byte array from server : My code is like this : server : Socket sock=null; ByteArrayOutputStream input=null;

  • How to Send a Multidimensional Array using Rack::Test post 2011-06-17

    I am sending a multidimensional array using the post method given by the Rack::Test like post "#{url}.json",:data => [["Company","Website"],["this is the dummy text, with,comma","www.technology.com"],["some company","www.url.com"]] But in my contr

  • Sending a String array through a tigase server from one Android to another using XMPP protocol 2011-07-01

    I'm currently trying to use the Smack libraries and the Tigase server to send a String array from one Android to another using the XMPP protocol (I'm developing in Java with the Eclipse IDE). is the String array an Item? or something else? I might be

  • Proper way to create packets along with sending and receiving them in socket programming using C 2011-07-22

    I had written a small client-server code where in I was sending integers and characters from my client to server. So I know the basics of socket programming in C like the steps to follow and all. Now I want to create a packet and send it to my server

  • PHP thinking OOP : sending and receiving Message : am I getting it right? 2011-08-14

    See Updates in the end : The current code-base has 1.4k line of purely procedural code which sends sms (has business logic, db connectivity, and everything in one gigantic if conditional nested with countless more ifs, no functions, full of literals,

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.693 (s). 11 q(s)