How to Make PATCH Requests with Axios

Jan 15, 2021

The easiest way to make a PATCH request with Axios is the axios.patch() function. The first parameter is the url to which the request will be made, and the second parameter is the data you will be sending to change. You can make a PATCH request with axios as follows:

const res = await axios.patch('https://httpbin.org/patch', { firstName: 'MasteringJS' });

res.data.headers['Content-Type']; //application/json;charset=utf-8

If the second parameter is an object, axios will do a JSON.stringify on the object before sending the request. It also will specify the content-type to application/json, allowing for smooth integration in most projects.

const res = await axios.patch('https://httpbin.org/patch', { id: 12345 });

res.data.headers['Content-Type']; //application/json;charset=utf-8

If you pass a string as the second parameter, axios will set the content-type header to application/x-www-form-urlencoded. This will turn the request body into a series of key-value pairs.

const res = await axios.patch('https://httpbin.org/patch', 'hello=world');

res.data.headers['Content-Type']; // application/x-www-form-urlencoded
res.data.json; // { hello: 'world' }

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Axios Tutorials