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

Why do we have to use querystring module? #739

Closed
c0debreaker opened this issue Mar 4, 2017 · 3 comments
Closed

Why do we have to use querystring module? #739

c0debreaker opened this issue Mar 4, 2017 · 3 comments

Comments

@c0debreaker
Copy link

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.

@uvtzxpm
Copy link

uvtzxpm commented Mar 4, 2017

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.

@c0debreaker
Copy link
Author

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

@omidkrad
Copy link

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
  },

@axios axios locked and limited conversation to collaborators May 22, 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