diff --git a/README.md b/README.md index 05101872a..c8d779f04 100644 --- a/README.md +++ b/README.md @@ -5924,7 +5924,7 @@ 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, callback, data = null, err = console.error) => { +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'); @@ -5947,8 +5947,8 @@ const newPost = { const data = JSON.stringify(newPost); httpPost( 'https://jsonplaceholder.typicode.com/posts', - console.log, - data + data, + console.log ); /* Logs: { "userId": 1, @@ -5957,6 +5957,15 @@ Logs: { "body": "bar bar bar" } */ +httpPost( + 'https://jsonplaceholder.typicode.com/posts', + null, //does not send a body + console.log +); /* +Logs: { + "id": 101 +} +*/ ``` diff --git a/docs/index.html b/docs/index.html index f11855dff..6aa025fbc 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1325,7 +1325,7 @@ Logs: { "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" } */ -

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 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.

const httpPost = (url, callback, data = null, err = console.error) => {
+

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 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.

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');
@@ -1342,8 +1342,8 @@ Logs: {
 const data = JSON.stringify(newPost);
 httpPost(
   'https://jsonplaceholder.typicode.com/posts',
-  console.log,
-  data
+  data,
+  console.log
 ); /*
 Logs: {
   "userId": 1,
@@ -1352,6 +1352,15 @@ Logs: {
   "body": "bar bar bar"
 }
 */
+httpPost(
+  'https://jsonplaceholder.typicode.com/posts',
+  null, //does not send a body
+  console.log
+); /*
+Logs: {
+  "id": 101
+}
+*/
 

parseCookie

Parse an HTTP Cookie header string and return an object of all cookie name-value pairs.

Use String.split(';') to separate key-value pairs from each other. Use Array.map() and String.split('=') to separate keys from values in each pair. Use Array.reduce() and decodeURIComponent() to create an object with all key-value pairs.

const parseCookie = str =>
   str
     .split(';')