diff --git a/snippets/httpDelete b/snippets/httpDelete deleted file mode 100644 index e69de29bb..000000000 diff --git a/snippets/httpDelete.md b/snippets/httpDelete.md new file mode 100644 index 000000000..34a42d5df --- /dev/null +++ b/snippets/httpDelete.md @@ -0,0 +1,24 @@ +### httpDelete + +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://website.com/users/123', request => { + console.log(request.responseText); +}); // 'Deletes a user from the database' +``` diff --git a/snippets/httpPost.md b/snippets/httpPost.md index cb395b7c5..40eb5ec59 100644 --- a/snippets/httpPost.md +++ b/snippets/httpPost.md @@ -16,7 +16,7 @@ const httpPost = (url, data, callback, err = console.error) => { request.onload = () => callback(request); request.onerror = () => err(request); request.send(data); -} +}; ``` ```js diff --git a/snippets/httpPut.md b/snippets/httpPut.md index 34a42d5df..4f9dbd785 100644 --- a/snippets/httpPut.md +++ b/snippets/httpPut.md @@ -1,24 +1,28 @@ -### httpDelete +### httpPut -Makes a `DELETE` request to the passed URL. +Makes a `PUT` request to the passed URL. -Use `XMLHttpRequest` web api to make a `delete` request to the given `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 third argument, `err` to log the request to the console's error stream by default. +Omit the last 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(); +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 -httpDelete('https://website.com/users/123', request => { +const password = "fooBaz"; +const data = JSON.stringify(password); +httpPut('https://website.com/users/123', data, request => { console.log(request.responseText); -}); // 'Deletes a user from the database' +}); // 'Updates a user's password in database' ``` diff --git a/tag_database b/tag_database index deea5bc57..4b1fb6565 100644 --- a/tag_database +++ b/tag_database @@ -61,8 +61,10 @@ hasFlags:node head:array hexToRGB:utility,string,math,advanced hide:browser,css +httpDelete:utility,url httpGet:utility,url httpPost:utility,url +httpPut:utility,url httpsRedirect:browser,url indexOfAll:array initial:array