Skip to content
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

New config param: arrayFormat ? #1443

Closed
today- opened this issue Mar 30, 2018 · 14 comments
Closed

New config param: arrayFormat ? #1443

today- opened this issue Mar 30, 2018 · 14 comments

Comments

@today-
Copy link

today- commented Mar 30, 2018

Summary

Now when i send an array in GET request
axios.get('/url', { id: [1, 2, 3] })
I receive this path: /url?id[]=1&id[]=2id[]=3 and it's not configurable.

But some target servers (with strong typed languages) wants another format (without braces after param name): /url?id=1&id=2id=3

https://stackoverflow.com/questions/3061273/send-an-array-with-an-http-get

And now I have to use paramsSerializer (right this case is mentioned in README), and even to use another package (Qs).

But I found in buildURL function than brackets is added forcibly, and it's quite easy to add options param to handle it.

https://github.com/axios/axios/blob/master/lib/helpers/buildURL.js#L43

Context

  • axios version: 0.18.0
  • Environment: chrome 65
@JustinBeckwith
Copy link
Collaborator

FWIW, I needed to swap out the default qs serializer in here for qs as well. This would be a valuable feature IMO.

@mattsawyer77
Copy link

On node 10.6, I just tried using the built-in querystring API for the paramsSerializer, and it seems to encode the array correctly.

https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options

@pzanitti
Copy link

If there is no standard/RFC that defines this, why was it decided that [] should be used? I understand a custom paramsSerializer can be implemented, but the current implementation shouldn't be opinionated.

I used this solution, unfortunately it requires an additional package for something that perhaps can be fixed with a simple toggle config.
#559 (comment)

@AuthorProxy
Copy link

up

@parker789
Copy link

Also would like to see a better solution for this.

@chinesedfan
Copy link
Collaborator

Really understand that users want everything out-of-box. But axios has its own design principle, which means simple and focused. Hope someone can list where those two formats come from, and let's choose the more standard one.

@determin1st
Copy link

determin1st commented Apr 1, 2020

the better solution is to use JSON arrays with application/json content-type.
Especially, new servers (no matter typed or not) should use more advanced (than form enctype) exchange..

@jasonsaayman
Copy link
Member

On this I don't think this should be implement as mentioned previously if you'd like to use another format, you can set a custom paramsSerializer as follows:

axios.defaults.paramsSerializer: function(params) {
  // return a query string
}

Using the qs library:

axios.defaults.paramsSerializer: function(params) {
  return qs.stringify(params, { indices: false }); // param=value1&param=value2
}

@codingyourlife
Copy link

codingyourlife commented Jan 24, 2022

I have a problem with the by @jasonsaayman mentioned approach...

param=value1&param=value2 on C# backend correctly maps to param[] array. but param=value1 does not!

Not sure yet how to solve this. Anyone?

Update: Nevermind. While debugging I figured out the actual issue was something completely different ^^

@anton-johansson
Copy link

On this I don't think this should be implement as mentioned previously if you'd like to use another format, you can set a custom paramsSerializer as follows:

I'm not sure I fully agree. There is nothing in the specification that says that these parameters should be suffixed with brackets (neither is there anything that says that it shouldn't), which makes this very opinionated (as mentioned previously).

Fixing this by adding the qs package works, but it feels like an unnecessary thing to do when it would be a very simple configuration flag in Axios itself. The fewer dependencies the better, in my view.

I could rely on the native Node.js querystring package, but there's two issues with that:

  • I'm using TypeScript, and the native function does not accept any, which is what Axios "provides".
  • It's deprecated in favor of the URLSearchParams API, which also does not seem to work well with an any object.

I could also replicate the entire function that Axios has by default, but that also feels like a very inconvenient way of handling this.

Could you consider re-opening this issue? 😃

@bertho-zero
Copy link

I already had tried something like this: #1565

But same response: use qs.

@julianmarmier
Copy link

For those like me that are just now stumbling upon this thread, the bracketless format can now be configured:

axios.defaults.paramsSerializer = {
  indexes: null,
};

@jasonsaayman
Copy link
Member

need to check if this in the docs @DavidJDallas

@jasonsaayman
Copy link
Member

closing due to being very stale, if this is still relevant please open a new issue

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

No branches or pull requests