Categories
JavaScript Answers

How to construct Http Post URL with form params with the JavaScript Axios Http client?

Spread the love

To construct Http Post URL with form params with the JavaScript Axios Http client, we use the querystring module.

For instance, we write

const querystring = require("querystring");
//...
await axios.post(
  authServerUrl,
  querystring.stringify({
    username: "abcd",
    password: "1235!",
    client_id: "user-client",
  }),
  {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
  }
);

to call post with the authServerUrl and a query string we get by calling querystring.stringiy with an object with the query parameters.

And we set the request headers by setting headers to an object with the headers.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *