From f4176de38467f63edd2be9f9c2ac50022cca61d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 8 Jan 2018 17:21:52 +0100 Subject: [PATCH 01/18] add httpGet --- snippets/httpGet.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 snippets/httpGet.md diff --git a/snippets/httpGet.md b/snippets/httpGet.md new file mode 100644 index 000000000..48eee97a1 --- /dev/null +++ b/snippets/httpGet.md @@ -0,0 +1,21 @@ +### httpGet + +Makes a `GET` request to the passed `URL` using `XMLHttpRequest` web api. + +Explain briefly how the snippet works. + +```js +const httpGet = url => { + const request = new XMLHttpRequest(); + request.open("GET", url, true); + request.onload = () => { + const response = JSON.parse(request.responseText); + (request.readyState === 4 && request.status == "200") ? console.log(response) : console.error(response); + } + request.send(); +} +``` + +```js +functionName('sampleInput') // 'sampleOutput' +``` From d74dcbc9fc798021e64ce74e328a2e50a397dfc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 8 Jan 2018 17:57:15 +0100 Subject: [PATCH 02/18] update httpGet --- snippets/httpGet.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/snippets/httpGet.md b/snippets/httpGet.md index 48eee97a1..a24dafac5 100644 --- a/snippets/httpGet.md +++ b/snippets/httpGet.md @@ -1,21 +1,20 @@ ### httpGet -Makes a `GET` request to the passed `URL` using `XMLHttpRequest` web api. +Makes a `GET` request to the passed `URL`. -Explain briefly how the snippet works. +Use `XMLHttpRequest` web api to retrieve data from the server. When the response is ready call the callback function. ```js -const httpGet = url => { +const httpGet = (url, callback) => { const request = new XMLHttpRequest(); request.open("GET", url, true); - request.onload = () => { - const response = JSON.parse(request.responseText); - (request.readyState === 4 && request.status == "200") ? console.log(response) : console.error(response); - } + request.onload = () => callback(request); request.send(); } ``` ```js -functionName('sampleInput') // 'sampleOutput' +httpGet('https://jsonplaceholder.typicode.com/posts', request => { + console.log(request.responseText); +}) // 'Array of 100 items' ``` From c7566e295ef84fdcc22110f0cfee02e5cbf66373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 8 Jan 2018 17:58:25 +0100 Subject: [PATCH 03/18] run tagger for httpGet --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index d5e39dfac..67d98d513 100644 --- a/tag_database +++ b/tag_database @@ -61,6 +61,7 @@ hasFlags:node head:array hexToRGB:utility,string,math,advanced hide:browser,css +httpGet:utility,url httpsRedirect:browser,url indexOfAll:array initial:array From 120cf1cad075c27f4f9c01a55f2406be295d83d8 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 8 Jan 2018 19:07:56 +0200 Subject: [PATCH 04/18] Added error handling Added handling for `onerror` to log on the `error` stream of the console by default, maybe not the best possible behavior, but good for starters. If you have a better idea for this, please change it accordingly. --- snippets/httpGet.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/snippets/httpGet.md b/snippets/httpGet.md index a24dafac5..cc8e6c1d7 100644 --- a/snippets/httpGet.md +++ b/snippets/httpGet.md @@ -1,14 +1,18 @@ ### httpGet -Makes a `GET` request to the passed `URL`. +Makes a `GET` request to the passed URL. -Use `XMLHttpRequest` web api to retrieve data from the server. When the response is ready call the callback function. +Use `XMLHttpRequest` web api to retrieve data from 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 httpGet = (url, callback) => { +const httpGet = (url, callback, err = console.error) => { const request = new XMLHttpRequest(); request.open("GET", url, true); request.onload = () => callback(request); + request.onerror = () => err(request); request.send(); } ``` From 62e4bfe6e7b243ac482ff1123cdbc04f1ef53b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 8 Jan 2018 21:07:02 +0100 Subject: [PATCH 05/18] add httpPost, update examples for http snippets --- snippets/httpGet.md | 6 +++--- snippets/httpPost.md | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 snippets/httpPost.md diff --git a/snippets/httpGet.md b/snippets/httpGet.md index cc8e6c1d7..1d183312c 100644 --- a/snippets/httpGet.md +++ b/snippets/httpGet.md @@ -2,7 +2,7 @@ Makes a `GET` request to the passed URL. -Use `XMLHttpRequest` web api to retrieve data from the given `url`. +Use `XMLHttpRequest` web api to make a `get` 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. @@ -18,7 +18,7 @@ const httpGet = (url, callback, err = console.error) => { ``` ```js -httpGet('https://jsonplaceholder.typicode.com/posts', request => { +httpGet('https://website.com/posts', request => { console.log(request.responseText); -}) // 'Array of 100 items' +}); // 'Retrieves all users from the database' ``` diff --git a/snippets/httpPost.md b/snippets/httpPost.md new file mode 100644 index 000000000..cb395b7c5 --- /dev/null +++ b/snippets/httpPost.md @@ -0,0 +1,31 @@ +### httpPost + +Makes a `POST` request to the passed URL. + +Use `XMLHttpRequest` web api to make a `post` 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 httpPost = (url, data, callback, err = console.error) => { + const request = new XMLHttpRequest(); + request.open("POST", url, true); + request.setRequestHeader('Content-type','application/json; charset=utf-8'); + request.onload = () => callback(request); + request.onerror = () => err(request); + request.send(data); +} +``` + +```js +const user = { + name: "Foo", + password: "fooBar" +}; +const data = JSON.stringify(user); +httpPost('https://website.com/posts', data, request => { + console.log(request.responseText); +}); // 'Makes a new instance of user in database' +``` From 5edb4836e77744332273b6db9b9fc7979ea94950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 8 Jan 2018 21:08:04 +0100 Subject: [PATCH 06/18] update tag_database and add semicolons to http snippets --- snippets/httpGet.md | 2 +- tag_database | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/snippets/httpGet.md b/snippets/httpGet.md index 1d183312c..565e6882d 100644 --- a/snippets/httpGet.md +++ b/snippets/httpGet.md @@ -14,7 +14,7 @@ const httpGet = (url, callback, err = console.error) => { request.onload = () => callback(request); request.onerror = () => err(request); request.send(); -} +}; ``` ```js diff --git a/tag_database b/tag_database index 67d98d513..deea5bc57 100644 --- a/tag_database +++ b/tag_database @@ -62,6 +62,7 @@ head:array hexToRGB:utility,string,math,advanced hide:browser,css httpGet:utility,url +httpPost:utility,url httpsRedirect:browser,url indexOfAll:array initial:array From c0bd173246f2f0a5b1e12585b45dec80a9b822d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 8 Jan 2018 21:16:14 +0100 Subject: [PATCH 07/18] add httpPut --- snippets/httpPut.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 snippets/httpPut.md diff --git a/snippets/httpPut.md b/snippets/httpPut.md new file mode 100644 index 000000000..98b1c91e7 --- /dev/null +++ b/snippets/httpPut.md @@ -0,0 +1,28 @@ +### httpPost + +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("POST", 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(password); +httpPost(`https://website.com/posts/123`, data, request => { + console.log(request.responseText); +}); // 'Updates a user's password in database' +``` From 757580f23fcda7cecbd2f8ef2441e2c7934cdabd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 8 Jan 2018 21:17:52 +0100 Subject: [PATCH 08/18] fix snippet typo and update examples to be consistent --- snippets/httpGet.md | 2 +- snippets/httpPut.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/httpGet.md b/snippets/httpGet.md index 565e6882d..f863b998c 100644 --- a/snippets/httpGet.md +++ b/snippets/httpGet.md @@ -18,7 +18,7 @@ const httpGet = (url, callback, err = console.error) => { ``` ```js -httpGet('https://website.com/posts', request => { +httpGet('https://website.com/users', request => { console.log(request.responseText); }); // 'Retrieves all users from the database' ``` diff --git a/snippets/httpPut.md b/snippets/httpPut.md index 98b1c91e7..6946b6ff1 100644 --- a/snippets/httpPut.md +++ b/snippets/httpPut.md @@ -11,7 +11,7 @@ Omit the last argument, `err` to log the request to the console's error stream b ```js const httpPut = (url, data, callback, err = console.error) => { const request = new XMLHttpRequest(); - request.open("POST", url, true); + request.open("PUT", url, true); request.setRequestHeader('Content-type','application/json; charset=utf-8'); request.onload = () => callback(request); request.onerror = () => err(request); @@ -22,7 +22,7 @@ const httpPut = (url, data, callback, err = console.error) => { ```js const password = "fooBaz"; const data = JSON.stringify(password); -httpPost(`https://website.com/posts/123`, data, request => { +httpPost(`https://website.com/users/123`, data, request => { console.log(request.responseText); }); // 'Updates a user's password in database' ``` From 567575c12748f29534ba9c1296bd4c2a99fa5b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 8 Jan 2018 21:29:25 +0100 Subject: [PATCH 09/18] add httpDelete hopefully last typos I made --- snippets/httpDelete | 0 snippets/httpPut.md | 28 ++++++++++++---------------- 2 files changed, 12 insertions(+), 16 deletions(-) create mode 100644 snippets/httpDelete diff --git a/snippets/httpDelete b/snippets/httpDelete new file mode 100644 index 000000000..e69de29bb diff --git a/snippets/httpPut.md b/snippets/httpPut.md index 6946b6ff1..34a42d5df 100644 --- a/snippets/httpPut.md +++ b/snippets/httpPut.md @@ -1,28 +1,24 @@ -### httpPost +### httpDelete -Makes a `PUT` request to the passed URL. +Makes a `DELETE` 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. +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 last argument, `err` to log the request to the console's error stream by default. +Omit the third 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); +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 -const password = "fooBaz"; -const data = JSON.stringify(password); -httpPost(`https://website.com/users/123`, data, request => { +httpDelete('https://website.com/users/123', request => { console.log(request.responseText); -}); // 'Updates a user's password in database' +}); // 'Deletes a user from the database' ``` From 755fe634ee37d8adc6ab354badb516f6a4de38a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 8 Jan 2018 21:31:30 +0100 Subject: [PATCH 10/18] trouble with GitKraken, fix everything now --- snippets/httpDelete | 0 snippets/httpDelete.md | 24 ++++++++++++++++++++++++ snippets/httpPost.md | 2 +- snippets/httpPut.md | 28 ++++++++++++++++------------ tag_database | 2 ++ 5 files changed, 43 insertions(+), 13 deletions(-) delete mode 100644 snippets/httpDelete create mode 100644 snippets/httpDelete.md 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 From a59ae52d4a4992259d1f3618500fb296a0770dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 8 Jan 2018 21:53:29 +0100 Subject: [PATCH 11/18] update tag_database Added browser to every http request --- tag_database | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tag_database b/tag_database index 4b1fb6565..6de2c2662 100644 --- a/tag_database +++ b/tag_database @@ -61,10 +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 +httpDelete:utility,url,browser +httpGet:utility,url,browser +httpPost:utility,url,browser +httpPut:utility,url,browser httpsRedirect:browser,url indexOfAll:array initial:array From 0dfd14a1e6b9d8a7b84840c472a43bcfde8e0f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Wed, 10 Jan 2018 19:18:54 +0100 Subject: [PATCH 12/18] remove Put & Delete http snippets --- snippets/httpDelete.md | 24 ------------------------ snippets/httpPut.md | 28 ---------------------------- 2 files changed, 52 deletions(-) delete mode 100644 snippets/httpDelete.md delete mode 100644 snippets/httpPut.md diff --git a/snippets/httpDelete.md b/snippets/httpDelete.md deleted file mode 100644 index 34a42d5df..000000000 --- a/snippets/httpDelete.md +++ /dev/null @@ -1,24 +0,0 @@ -### 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/httpPut.md b/snippets/httpPut.md deleted file mode 100644 index 4f9dbd785..000000000 --- a/snippets/httpPut.md +++ /dev/null @@ -1,28 +0,0 @@ -### httpPut - -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(password); -httpPut('https://website.com/users/123', data, request => { - console.log(request.responseText); -}); // 'Updates a user's password in database' -``` From d3b14b4411a929a052fee1707118856dcecd963d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Wed, 10 Jan 2018 19:23:26 +0100 Subject: [PATCH 13/18] add to archive --- snippets_archive/httpDelete.md | 24 ++++++++++++++++++++++++ snippets_archive/httpPut.md | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 snippets_archive/httpDelete.md create mode 100644 snippets_archive/httpPut.md diff --git a/snippets_archive/httpDelete.md b/snippets_archive/httpDelete.md new file mode 100644 index 000000000..34a42d5df --- /dev/null +++ b/snippets_archive/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_archive/httpPut.md b/snippets_archive/httpPut.md new file mode 100644 index 000000000..4f9dbd785 --- /dev/null +++ b/snippets_archive/httpPut.md @@ -0,0 +1,28 @@ +### httpPut + +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(password); +httpPut('https://website.com/users/123', data, request => { + console.log(request.responseText); +}); // 'Updates a user's password in database' +``` From 0ca872484283106fc4d173a95edbecac5f854222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Wed, 10 Jan 2018 19:43:29 +0100 Subject: [PATCH 14/18] update httpGet --- snippets/httpGet.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/snippets/httpGet.md b/snippets/httpGet.md index f863b998c..20ea015cd 100644 --- a/snippets/httpGet.md +++ b/snippets/httpGet.md @@ -3,22 +3,19 @@ Makes a `GET` request to the passed URL. Use `XMLHttpRequest` web api to make a `get` request to the given `url`. -Handle the `onload` event, by running the provided `callback` function. +Handle the `onload` event, by console logging the `responseText`. 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 httpGet = (url, callback, err = console.error) => { +const httpGet = (url, err = console.error) => { const request = new XMLHttpRequest(); request.open("GET", url, true); - request.onload = () => callback(request); + request.onload = () => console.log(request.responseText); request.onerror = () => err(request); request.send(); }; ``` ```js -httpGet('https://website.com/users', request => { - console.log(request.responseText); -}); // 'Retrieves all users from the database' +httpGet('https://jsonplaceholder.typicode.com/posts'); // 'Console logs JSON of 100 posts' ``` From 329255aac676cca8d188c49877008338d6f1e273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Wed, 10 Jan 2018 19:53:36 +0100 Subject: [PATCH 15/18] update tag_database, update httpPost --- snippets/httpPost.md | 9 ++++----- tag_database | 2 -- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/snippets/httpPost.md b/snippets/httpPost.md index 40eb5ec59..ffa11a633 100644 --- a/snippets/httpPost.md +++ b/snippets/httpPost.md @@ -4,16 +4,15 @@ Makes a `POST` request to the passed URL. Use `XMLHttpRequest` web api to make a `post` 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 `onload` event, by console logging the `responseText`. 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 httpPost = (url, data, callback, err = console.error) => { +const httpPost = (url, data, err = console.error) => { const request = new XMLHttpRequest(); request.open("POST", url, true); request.setRequestHeader('Content-type','application/json; charset=utf-8'); - request.onload = () => callback(request); + request.onload = () => console.log(request.responseText); request.onerror = () => err(request); request.send(data); }; @@ -27,5 +26,5 @@ const user = { const data = JSON.stringify(user); httpPost('https://website.com/posts', data, request => { console.log(request.responseText); -}); // 'Makes a new instance of user in database' +}); // '' ``` diff --git a/tag_database b/tag_database index 6de2c2662..8e768751d 100644 --- a/tag_database +++ b/tag_database @@ -61,10 +61,8 @@ hasFlags:node head:array hexToRGB:utility,string,math,advanced hide:browser,css -httpDelete:utility,url,browser httpGet:utility,url,browser httpPost:utility,url,browser -httpPut:utility,url,browser httpsRedirect:browser,url indexOfAll:array initial:array From 6461737d1fb6adb732b862ee834e7b4c7cbb6002 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 10 Jan 2018 21:22:22 +0200 Subject: [PATCH 16/18] Update httpGet.md --- snippets/httpGet.md | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/snippets/httpGet.md b/snippets/httpGet.md index 20ea015cd..abd31ac15 100644 --- a/snippets/httpGet.md +++ b/snippets/httpGet.md @@ -2,20 +2,28 @@ Makes a `GET` request to the passed URL. -Use `XMLHttpRequest` web api to make a `get` request to the given `url`. -Handle the `onload` event, by console logging the `responseText`. +Use [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest) web api to make a `get` request to the given `url`. +Handle the `onload` event, by calling the given `callback` the `responseText`. Handle the `onerror` event, by running the provided `err` function. +Omit the third argument, `err`, to log errors to the console's `error` stream by default. ```js -const httpGet = (url, err = console.error) => { +const httpGet = (url, callback, err = console.error) => { const request = new XMLHttpRequest(); request.open("GET", url, true); - request.onload = () => console.log(request.responseText); + request.onload = () => callback(request.responseText); request.onerror = () => err(request); request.send(); }; ``` ```js -httpGet('https://jsonplaceholder.typicode.com/posts'); // 'Console logs JSON of 100 posts' +httpGet('https://jsonplaceholder.typicode.com/posts/1', console.log); /* +Logs: { + "userId": 1, + "id": 1, + "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", + "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" +} +*/ ``` From 31181db31b0b424b5d3f83188816569725f8ed69 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 10 Jan 2018 21:31:28 +0200 Subject: [PATCH 17/18] Update httpPost.md --- snippets/httpPost.md | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/snippets/httpPost.md b/snippets/httpPost.md index ffa11a633..f0f7426da 100644 --- a/snippets/httpPost.md +++ b/snippets/httpPost.md @@ -2,29 +2,38 @@ Makes a `POST` request to the passed URL. -Use `XMLHttpRequest` web api to make a `post` request to the given `url`. +Use [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest) web api to make a `post` request to the given `url`. Set the value of an `HTTP` request header with `setRequestHeader` method. -Handle the `onload` event, by console logging the `responseText`. +Handle the `onload` event, by calling the given `callback` the `responseText`. Handle the `onerror` event, by running the provided `err` function. +Omit the third argument, `data`, to send no data to the provided `url`. +Omit the fourth argument, `err`, to log errors to the console's `error` stream by default. ```js -const httpPost = (url, data, err = console.error) => { +const httpPost = (url, callback, data = null, err = console.error) => { const request = new XMLHttpRequest(); request.open("POST", url, true); request.setRequestHeader('Content-type','application/json; charset=utf-8'); - request.onload = () => console.log(request.responseText); + request.onload = () => callback(request.responseText); request.onerror = () => err(request); request.send(data); }; ``` ```js -const user = { - name: "Foo", - password: "fooBar" +const newPost = { + "userId": 1, + "id": 1337, + "title": "Foo", + "body": "bar bar bar" }; -const data = JSON.stringify(user); -httpPost('https://website.com/posts', data, request => { - console.log(request.responseText); -}); // '' +const data = JSON.stringify(newPost); +httpPost('https://website.com/posts', console.log, data; /* +Logs: { + "userId": 1, + "id": 1337, + "title": "Foo", + "body": "bar bar bar" +} +*/ ``` From 71a907d53d1d2c35c9e13c0c430609e8a12489a1 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 10 Jan 2018 21:31:48 +0200 Subject: [PATCH 18/18] Update httpPost.md --- snippets/httpPost.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/httpPost.md b/snippets/httpPost.md index f0f7426da..ae5c43469 100644 --- a/snippets/httpPost.md +++ b/snippets/httpPost.md @@ -28,7 +28,7 @@ const newPost = { "body": "bar bar bar" }; const data = JSON.stringify(newPost); -httpPost('https://website.com/posts', console.log, data; /* +httpPost('https://jsonplaceholder.typicode.com/posts', console.log, data; /* Logs: { "userId": 1, "id": 1337,