-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect parsing of complex JS nested objects in POST request #400
Comments
Are you sure the server is returning the JSON data in the correct format? Needle simply does a |
looks to me like this happens when the post data is sent in querystring format (default). use i would refrain from using querystring format to transfer complex objects, because to my knowledge there is no consistant standard about how to encode and decode complex objects. see for yourself: const qs = require("querystring");
const data = {
field1: 'hello',
field2: [
'line1',
'line2',
'line3',
],
field3: [
{
subfield1: 'this is a string',
subfield2: ['this is array'],
subfield3: ['another array']
},
{
subfield1: 'this is a string',
subfield2: ['this is array'],
subfield3: ['another array']
},
{
subfield1: 'this is a string',
subfield2: ['this is array'],
subfield3: ['another array']
},
]
};
console.log(qs.decode(qs.encode(data))); needle uses this method to encode the data, and the express.urlencoded middleware that ultimately parses the data comes from here. |
Oh I see. I misunderstood then; I thought the problem was related the the decoding of JSON, not the encoding part. Ok I'll take a look in a while. |
Ok, we can try and see whether adding the array item index this line makes it works like expected. The resulting encoded string would look like this (using your example data): |
maybe writing a test utilizing the body-parser middleware from express would be in order. keep in mind that this would fix it for one kind of backend, other backends like php might end up not parsing it in the same way. |
I created a simple Express server with one POST endpoint. The POST endpoint just return the request body sent from the client. The code for this server looks like this:
I also added a script file to test the said POST endpoint using
needle
. This is the code for the script file:Now, the output of
console.log(res.body.data)
does not match with the request body that was sent originally to the server.field3
becomes an array with single object instead of three and thesubfield1
becomes an array. The output looks like this:Now, to verify that the
needle
output is incorrect, I tested it in Postman. The correct output should look like this:In summary, upon sending a complex nested objects in
needle
via POST request, it changes the structure of the original request body. To replicate the issue, this is my environment:node --version v16.14.2
npm --version 8.5.0
The text was updated successfully, but these errors were encountered: