question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.type('form') doesn't prepare Javascript arrays correctly

See original GitHub issue

Please let me know if I am doing something wrong, but if I say something like:

var data = {
    interests: [300, 400]
}

request('/foo')
    .type('form')
    .send(data)
    .end()

The form data is sent as

interests: 300, 400

when it should be

interests[]: 300
interests[]: 400

I am able to get it to work if I do:

request('/foo')
    .type('form')
    .send($.param(data))
    .end()

but the reason I’m using superagent is so this will also work on Node and I’d rather not require in Node jQuery just for this.

With that said, is there a setting I’m missing that is not preparing the params correctly?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kornelskicommented, Apr 27, 2016

This is intentional and IMHO correct.

The brackets [] are only an informal convention used by some frameworks/servers, but not all environments use it. There are web servers that expect repeated keys without brackets:

 http://example.com/service?value=1&value=2&value=3

If superagent always added [] to arrays, then it would not be able to create a URL like the one above.

The current implementation supports both cases. If you’d like an array with [] in the name, then put [] in the name explicitly:

request.get('/').query({'interests[]': [300, 400]});
0reactions
J-F-Liucommented, Apr 27, 2016

Please note that jQuery.param() also has special treatment for object data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

type('form') doesn't prepare Javascript arrays correctly #250
Please let me know if I am doing something wrong, but if I say something like: var data = { interests: [300, 400]...
Read more >
JS not pushing to array - javascript - Stack Overflow
Upon further inspection: the prepare() function is looking for an element with id="seatsArray", which doesn't exist.
Read more >
Discover JavaScript Arrays & How to Use Them - HubSpot Blog
Discover JavaScript arrays, how they work and what that means for your software ... What is the correct way to write a JavaScript...
Read more >
JavaScript Arrays - tips, tricks and examples - CodinGame
Arrays are just regular objects​​ In Javascript, there are only 6 data types defined – the primitives (boolean, number, string, null, undefined) and...
Read more >
Check if the value exists in Array in Javascript - Javatpoint
In a programming language like Javascript, to check if the value exists in an array, there are certain methods. To be precise, there...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found