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

Get parameter array with key value #1093

Closed
fer-ri opened this issue Sep 20, 2017 · 3 comments
Closed

Get parameter array with key value #1093

fer-ri opened this issue Sep 20, 2017 · 3 comments

Comments

@fer-ri
Copy link

fer-ri commented Sep 20, 2017

Hi,

I've searching, but can't find the way to do it. Basically i want to send GET request with array parameter like this

GET /comments?filter[post]=3&filter[author]=12

I've tried this

params: {
  filter: {post: 3, author: 12}
}

But still no luck :(

Any hint?

Thanks

@ngonzalvez
Copy link

ngonzalvez commented Sep 21, 2017

Hi @ghprod, first of all I want to make clear that I'm not a contributor to this library nor I use it, but I was passing by and I saw this issue and decided to investigate the code a bit.

Axios does not support that kind of encoding by default but it allows you to provide a custom serializer. You can accomplish this very easily with the following code:

// Axios include here...

axios.get('/user', {
    params: {
        filter: {post: 3, author: 12} 
    },
    paramsSerializer: customSerializerFunc
});

To make it simpler, I googled a bit for querystring serializer libraries and found qs which has support for browsers and nodejs. You can use it as a serializer this way:

// Include axios and qs here...

axios.get('/user', {
    params: {
        filter: {post: 3, author: 12} 
    },
    paramsSerializer: qs.stringify  // If this doesnt work, append .bind(qs) to the end of this line.
});

Maybe you'll see this: filter%5Bpost%5D=3 instead of filter[post]=3, that's because URL encoding is enabled. You can disable it this way:

axios.get('/user', {
    params: {
        filter: {post: 3, author: 12} 
    },
    paramsSerializer: function(params) {
        return qs.stringify(params, { encode: false });
    }
});

Let me know if it worked.
Regards.

@fer-ri
Copy link
Author

fer-ri commented Sep 21, 2017

It's works .. 👍

Thanks a lot @ngonzalvez

Have a great day :)

@fer-ri fer-ri closed this as completed Sep 21, 2017
@catheibasilio
Copy link

hi, i need help regarding axios -- how do i get a value from input and put it as a value on a parameter for axios.get ; say for a search function that i am building - part of the url is a query param.

here's the code
axios
.get('http://localhost:8080/customer/search?hint=da')

where hint = "searchText"

please help.

thanks!

@axios axios locked and limited conversation to collaborators May 22, 2020
LocoDelAssembly referenced this issue in SpeciesFileGroup/taxonworks Jul 2, 2020
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

3 participants