Migrate httpDelete, httpPut
This commit is contained in:
29
snippets/httpDelete.md
Normal file
29
snippets/httpDelete.md
Normal file
@ -0,0 +1,29 @@
|
||||
---
|
||||
title: httpDelete
|
||||
tags: utility,url,browser,intermediate
|
||||
---
|
||||
|
||||
Makes a `DELETE` request to the passed URL.
|
||||
|
||||
Use `XMLHttpRequest` web api to make a `delete` request to the given `url`.
|
||||
Handle the `onload` event, by running the provided `callback` function.
|
||||
Handle the `onerror` event, by running the provided `err` function.
|
||||
Omit the third argument, `err` to log the request to the console's error stream by default.
|
||||
|
||||
```js
|
||||
const httpDelete = (url, callback, err = console.error) => {
|
||||
const request = new XMLHttpRequest();
|
||||
request.open('DELETE', url, true);
|
||||
request.onload = () => callback(request);
|
||||
request.onerror = () => err(request);
|
||||
request.send();
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
httpDelete('https://jsonplaceholder.typicode.com/posts/1', request => {
|
||||
console.log(request.responseText);
|
||||
}); /*
|
||||
Logs: {}
|
||||
*/
|
||||
```
|
||||
43
snippets/httpPut.md
Normal file
43
snippets/httpPut.md
Normal file
@ -0,0 +1,43 @@
|
||||
---
|
||||
title: httpPut
|
||||
tags: utility,url,browser,intermediate
|
||||
---
|
||||
|
||||
Makes a `PUT` request to the passed URL.
|
||||
|
||||
Use `XMLHttpRequest` web api to make a `put` request to the given `url`.
|
||||
Set the value of an `HTTP` request header with `setRequestHeader` method.
|
||||
Handle the `onload` event, by running the provided `callback` function.
|
||||
Handle the `onerror` event, by running the provided `err` function.
|
||||
Omit the last argument, `err` to log the request to the console's error stream by default.
|
||||
|
||||
```js
|
||||
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);
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
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
|
||||
}
|
||||
*/
|
||||
```
|
||||
Reference in New Issue
Block a user