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('', { firstName: 'MasteringJS' });['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('', { id: 12345 });['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('', 'hello=world');['Content-Type']; // application/x-www-form-urlencoded; // { hello: 'world' }

More Axios Tutorials