By default, axios serializes JavaScript objects to JSON
. To send data in the application/x-www-form-urlencoded
format instead, you can use one of the following options.
# Browser
In a browser, you can use the URLSearchParams
(opens new window) API as follows:
const params = new URLSearchParams();
params.append('param1', 'value1');
params.append('param2', 'value2');
axios.post('/foo', params);
Note that
URLSearchParams
is not supported by all browsers (see caniuse.com (opens new window)), but there is a polyfill (opens new window) available (make sure to polyfill the global environment).
Alternatively, you can encode data using the qs
(opens new window) library:
const qs = require('qs');
axios.post('/foo', qs.stringify({ 'bar': 123 }));
Or in another way (ES6),
import qs from 'qs';
const data = { 'bar': 123 };
const options = {
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: qs.stringify(data),
url,
};
axios(options);
# Node.js
# Query string
In node.js, you can use the querystring
(opens new window) module as follows:
const querystring = require('querystring');
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
or 'URLSearchParams' (opens new window) from 'url module' (opens new window) as follows:
const url = require('url');
const params = new url.URLSearchParams({ foo: 'bar' });
axios.post('http://something.com/', params.toString());
You can also use the qs
(opens new window) library.
# NOTE
The qs
library is preferable if you need to stringify nested objects, as the querystring
method has known issues with that use case (https://github.com/nodejs/node-v0.x-archive/issues/1665).
# Form data
In node.js, you can use the form-data
(opens new window) library as follows:
const FormData = require('form-data');
const form = new FormData();
form.append('my_field', 'my value');
form.append('my_buffer', new Buffer(10));
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
axios.post('https://example.com', form, { headers: form.getHeaders() })
Alternatively, use an interceptor:
axios.interceptors.request.use(config => {
if (config.data instanceof FormData) {
Object.assign(config.headers, config.data.getHeaders());
}
return config;
});
← Cancellation Notes →