make a http patch request in javascript

Here's an example using the Fetch API in JavaScript:

index.tsx
fetch(url, {
  method: 'PATCH',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(data)
})
.then(response => {
  // handle response
})
.catch(error => {
  // handle error
});
205 chars
14 lines

Alternatively, here's an example using the older XMLHttpRequest API:

index.tsx
const xhr = new XMLHttpRequest();
xhr.open('PATCH', url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4) {
    // handle response
  }
};
xhr.send(JSON.stringify(data));
246 chars
10 lines

gistlibby LogSnag