GithubHelp home page GithubHelp logo

Comments (5)

tomas avatar tomas commented on July 20, 2024

Are you sure the server is returning the JSON data in the correct format? Needle simply does a JSON.parse on the response data so I'm not sure if Needle's the culprit in this one.

from needle.

yetzt avatar yetzt commented on July 20, 2024

looks to me like this happens when the post data is sent in querystring format (default). use { json: true } in your needle options to send the data as json.

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.

from needle.

tomas avatar tomas commented on July 20, 2024

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.

from needle.

tomas avatar tomas commented on July 20, 2024

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):

image

from needle.

yetzt avatar yetzt commented on July 20, 2024

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.

from needle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.