1.1 KiB
1.1 KiB
title, tags
| title | tags |
|---|---|
| httpPut | browser,intermediate |
Makes a PUT request to the passed URL.
- Use
XMLHttpRequestweb api to make aPUTrequest to the givenurl. - Set the value of an
HTTPrequest header withsetRequestHeadermethod. - Handle the
onloadevent, by running the providedcallbackfunction. - Handle the
onerrorevent, by running the providederrfunction. - Omit the last argument,
errto log the request to the console's error stream by default.
const httpPut = (url, data, callback, err = console.error) => {
const request = new XMLHttpRequest();
request.open('PUT', url, true);
request.setRequestHeader('Content-type', 'application/json; charset=utf-8');
request.onload = () => callback(request);
request.onerror = () => err(request);
request.send(data);
};
const password = 'fooBaz';
const data = JSON.stringify({
id: 1,
title: 'foo',
body: 'bar',
userId: 1
});
httpPut('https://jsonplaceholder.typicode.com/posts/1', data, request => {
console.log(request.responseText);
}); /*
Logs: {
id: 1,
title: 'foo',
body: 'bar',
userId: 1
}
*/