Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

$http get array params are sent incorrectly #3121

Closed
benschwarz opened this issue Jul 3, 2013 · 12 comments
Closed

$http get array params are sent incorrectly #3121

benschwarz opened this issue Jul 3, 2013 · 12 comments

Comments

@benschwarz
Copy link

If I make a request with:

params = {
  actors: ['Elvis', 'Jane', 'Frances']
}

The query string should be:

?actors[]=Elvis&actors[]=Jane&actors[]=Frances

Instead, its being set as:

?actors=Elvis&actors=Jane&actors=Frances

Turns out: Hashes aren't sent correctly either #2091

@lucipacurar
Copy link

I can confirm the issue. In AngularJS 1.0.6 the query string would have been: ?actors=Elvis,Jane,Frances.

@benschwarz
Copy link
Author

jQuery has managed HTTP beautifully since I can remember, perhaps we should take a peek into the test suite there before we discover any more surprises.

Otherwise, I'm happy to make patch, just need to know what to do — @IgorMinar @mhevery @vojtajina ?

@petebacondarwin
Copy link
Contributor

@benschwarz - Hi Ben. Thanks for submitting this issue.

There is no particular standard for how arrays should be serialized into query strings. Different back ends expect different formats. This current method (in 1.1.x) was chosen specifically because it allows the developer the most flexibility. See the PR for this: #2522

You can get your desired serialization by giving setting params to:

params = {
  "actors[]": ['Elvis', 'Jane', Frances']
};

In the long term we are looking to make the serialization of URL queries pluggable so that you will be able to specify your own strategy.

@benschwarz
Copy link
Author

@petebacondarwin ok… I thought that arrays were actors[]=value, myself… but I didn't realise that there are other common implementations.

Perhaps its worth adding to the docs how the params work for GET requests, and how one can achieve the desired result?

@petebacondarwin
Copy link
Contributor

@benschwarz - the latest version of the docs is a bit more clear.
http://ci.angularjs.org/view/AngularJS/job/angular.js-angular-master/lastSuccessfulBuild/artifact/build/docs/index.html#!/api/ng.$location
But I agree an example would be best.

@benschwarz
Copy link
Author

@petebacondarwin sounds like there is nothing to fix here. Thanks for the input.
How can we make sure that it makes it to the documentation?

@cidthecoatrack
Copy link

So the common workaround of appending"[]" to the array-type variable name doesn't work. I am using Angular 1.5.7, and when I give the following parameters:

params: {
    environment: 'Dungeon',
    level: 1,
    'filters[]': ['Dragon', 'Ooze']
};

I get the following url:

http://localhost:49200/Encounter/Validate?environment=Dungeon&filters%5B%5D=Dragon&filters%5B%5D=Ooze&level=1

It is so close to right - but Angular serializes the [], so the url is wrong.

@gkalpak
Copy link
Member

gkalpak commented Jul 20, 2016

This is a 3-year old issue, so things have evolved since then 😄
See #14933.

@lukasborawski
Copy link

@petebacondarwin thx, this resolved my problem, cheers

@yasir-rafiq
Copy link

what if I have to send array of objects?

@petebacondarwin
Copy link
Contributor

@yasir-rafiq - query params must be string based. If you want to send an array of objects then you will need to serialize the objects to strings first.

@yasir-rafiq
Copy link

thnks @petebacondarwin

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants