7

I was wondering how I would be able to use query-string npm package to ease my axios calls. The package I use is: https://www.npmjs.com/package/query-string

An example:

import qs from 'query-string';
import axios from 'axios';

axios.get(`http://localhost:3000/api/products${qs.parse({ offset: 0, limit: 12 })}`);

Not sure why but this does not work as expected.

3
  • 1
    “Not sure why but this does not work as expected” - because you’re going in exactly the wrong direction … parse parses an existing query string into the corresponding data structure - you want the opposite, you want to create the query string from your data structure. The method to do that, is queryString.stringify. Commented Nov 6, 2019 at 14:21
  • Oh, thank you so much @04FS ! Totally forgot that method. Cheers and have an awesome week ahead man! Commented Nov 6, 2019 at 14:22
  • Mobile devices can indeed produce backticks. On Android you can find the symbol keyboard, and then do a long-press on the apostrophe to get a menu to choose from several apostrophe-like symbols. Please repair this at the earliest opportunity, and for new questions, please do not post if you know you cannot format it correctly. Commented Nov 6, 2019 at 14:25

5 Answers 5

20

You don't really need it. Axios has a standard way to put params into your request without any additional libraries or doing something manually.

axios
    .request({
      url: '/some/url',
      method: 'get',
      params: {
        offset: 0,
        limit: 12,
        unknown: '???'
      },
      ...
    })

Must be converted to /some/url?offset=0&limit=12&unknown=%3F%3F%3F.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, yeah, i get that, just feel that wanna shorten the code a lil bit so that I won’t have to go through the trouble of writing: params: { offset, limit } in the parameters
@kyapwc parameters encoding is the standard behaviour of axios
6

As there is no need to use query-string as axios does it automatically by putting the params into the request.

But still if you want to use the query-string package you can do it by this way.

Here is a short example which can give you somewhat idea of using query-string.

import qs from 'qs'; (https://www.npmjs.com/package/qs)
import axios from 'axios';

export default axios.create({
    baseURL: `http://localhost:3000/api/products`,
    params: (params) => {
        return qs.stringify(params, {offset: 0, limit: 12});
    },
});

Comments

5

Axios provides another handy yet powerful way to send your queryParams as object to http GET method.

You may change your request this way:

axios.get('http://localhost:3000/api/products', {
    params: {
      offset: 0,
      limit: 12
    }
  })

Comments

3
this.$axios.get('/', {
    params: {},
    paramsSerializer: params => {
      return qs.stringify(params);
    }
  })

Comments

0

In order to use template literals, you need to use the back-tick () not normal quotes. CODE:

axios.get(`http://localhost:3000/api/products${qs.parse({ offset: 0, limit: 12 })}`);

If you dont want to use back-ticks, you cannot use ${} syntax. Just do it like normal string interpolation works.

axios.get(‘http://localhost:3000/api/products'+qs.parse({ offset: 0, limit: 12 })});

2 Comments

I wrote a short comment about the backticks bro. Using mobile to post the question, the keyboard does not have backticks haha
Yeah, thats why I said use normal string interpolation. Because you cannot use ${} with normal quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.