axios-axios/docs/pages/advanced/x-www-form-urlencoded-format.md
Jay 054c1f30fd
feat: unify docs to main repo (#10649)
* ci: set hardened --ignore-scripts for all ci actions

* docs: adds new docs platform

* chore: remove un-needed ignore

* chore: add sponsors data. adjust package.json to be of type module

* fix: inconsistency between the docs and readme

* fix: docs inconsistency

* docs: update language and phrasing

* style: fix issues with card styling

* docs: update security.md with latest changes

* docs: remove un-needed code

* docs: fix inconsistencies with actual library function

* ci: added deployment for docs

* chore: added axios as dep for docs

* docs: fix batch of errors

* fix: bump esbuild as the version included is a risk
2026-04-04 20:25:41 +02:00

2.9 KiB

x-www-form-urlencoded format

URLSearchParams

By default, axios serializes JavaScript objects to JSON. To send data in the application/x-www-form-urlencoded format instead, you can use the URLSearchParams API, which is supported in the vast majority of browsers,and Node starting with v10 (released in 2018).

const params = new URLSearchParams({ foo: 'bar' });
params.append('extraparam', 'value');
axios.post('/foo', params);

Query string

For older browsers or environments without URLSearchParams, you can use the qs library to serialize objects to the application/x-www-form-urlencoded format.

const qs = require('qs');
axios.post('/foo', qs.stringify({ bar: 123 }));

In very old versions of Node.js, you can use the built-in querystring module that ships with Node.js. Note that this module has been deprecated in Node.js v16 — prefer URLSearchParams or qs for new code.

const querystring = require('querystring');
axios.post('https://something.com/', querystring.stringify({ foo: 'bar' }));

Automatic serialization to URLSearchParams

Starting from v0.21.0, axios automatically serializes JavaScript objects to URLSearchParams if the Content-Type header is set to application/x-www-form-urlencoded. This means that you can pass a JavaScript object directly to the data property of the axios request config. For example when passing data to a POST request:

const data = {
  x: 1,
  arr: [1, 2, 3],
  arr2: [1, [2], 3],
  users: [
    { name: 'Peter', surname: 'Griffin' },
    { name: 'Thomas', surname: 'Anderson' },
  ],
};

await axios.postForm('https://postman-echo.com/post', data, {
  headers: { 'content-type': 'application/x-www-form-urlencoded' },
});

The data object will be automatically serialized to URLSearchParams and sent in the application/x-www-form-urlencoded format. The server will receive the following data:

{
  "x": "1",
  "arr[]": ["1", "2", "3"],
  "arr2[0]": "1",
  "arr2[1][0]": "2",
  "arr2[2]": "3",
  "users[0][name]": "Peter",
  "users[0][surname]": "Griffin",
  "users[1][name]": "Thomas",
  "users[1][surname]": "Anderson"
}

If your backend body-parser (like body-parser of express.js) supports nested objects decoding, you will get the same object on the server-side automatically

var app = express();

app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

app.post('/', function (req, res, next) {
  // echo body as JSON
  res.send(JSON.stringify(req.body));
});

server = app.listen(3000);