Axios: Why do we have to use querystring module?

Created on 4 Mar 2017  ·  3Comments  ·  Source: axios/axios

Spent almost whole day trying to figure out why Axios was sending my data in the wrong format until I found this http://stackoverflow.com/questions/31756756/axios-http-client-how-to-construct-http-post-url-with-form-params. The fix is to use querystring module.

axios.post(
  '/api/v1/auth/login',
  querystring.stringify({ // <-- this is what fixed it. JSON.stringify didn't help here
    user_id: this.state.user_id,
    password: this.state.password,
    _csrf: result.data.csrfToken
  }),
  {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
.then((res) => {
  if (res.data && res.data) {
    console.log('here')
  }
})
.catch((err) => {
  console.log(err);
});

However, I feel that Axios should take care of this.

Most helpful comment

You can use params for specifying query strings.

  // `params` are the URL parameters to be sent with the request
  // Must be a plain object or a URLSearchParams object
  params: {
    ID: 12345
  },

All 3 comments

There are already instructions in the readme, so it should have been quite easy to find the answer:

https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-format

Whether or not Axios should automatically take care of this isn't a question for me to answer, but the main focus of the library is sending JSON, so it optimizes for that use case I guess.

Oh, thank you for that link. I didn't see it before.

You can use params for specifying query strings.

  // `params` are the URL parameters to be sent with the request
  // Must be a plain object or a URLSearchParams object
  params: {
    ID: 12345
  },
Was this page helpful?
0 / 5 - 0 ratings

Related issues

altruisticsoftware picture altruisticsoftware  ·  3Comments

Baoyx007 picture Baoyx007  ·  3Comments

achingbrain picture achingbrain  ·  3Comments

StefH picture StefH  ·  3Comments

samayo picture samayo  ·  3Comments