Advertisement
GalinaKG

HTTP Methods: GET, CREATE, PATCH, DELETE

Apr 6th, 2023 (edited)
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // GET than - catch
  2. -----------------------
  3. fetch(BASE_URL)
  4.       .then((response) => response.json())
  5.       .then((data) => {
  6.           console.log(data)
  7.       })
  8.       .catch((error) => console.error(error));
  9.  
  10. // GET async - await
  11. -----------------------
  12.  let response = await fetch(BASE_URL);
  13.  let data = await response.json();
  14. ************************************************************************************************
  15. // POST than - catch
  16. -----------------------
  17.  fetch(BASE_URL, {
  18.           method: 'POST',
  19.           headers: {'Content-Type': 'application/json'},
  20.           body: JSON.stringify({ author, title })
  21.         })
  22. // const payload = JSON.stringify({
  23. //       product: product.value,
  24. //       count: count.value,
  25. //       price: price.value
  26. //     });
  27. //     const httpHeaders = {
  28. //       method: 'POST',
  29. //       body: payload
  30. //     };
  31.  
  32. //     fetch(BASE_URL, httpHeaders)
  33. //       .then(() => {
  34. //         loadAllProductsHandler();
  35. //         otherDOMSelectors.form.reset();
  36. //       })
  37. //       .catch((err) => {
  38. //         console.error(err);
  39. //       })
  40. //   }
  41. // POST async - await
  42. -----------------------
  43.  await fetch(BASE_URL, {
  44.         method: 'POST',
  45.         headers: {
  46.             'Content-Type': 'application/json'
  47.         },
  48.         body: JSON.stringify(data)
  49.     });
  50. ************************************************************************************************
  51. // PUT than - catch
  52. -----------------------
  53. fetch(`${BASE_URL}${id}`, {
  54.           method: 'PUT',
  55.           headers: {'Content-Type': 'application/json'},
  56.           body: JSON.stringify({
  57.             "author": changedAuthor,
  58.             "title": changedTitle
  59.           })
  60. ************************************************************************************************
  61. //  PATCH async -await
  62. -----------------------
  63.   await fetch(BASE_URL + id, {
  64.             method: 'PATCH',
  65.             headers: {
  66.                 'Content-Type': 'application/json'
  67.             },
  68.             body: JSON.stringify(data)
  69.         })
  70. // PATCH than - catch
  71. -----------------------
  72.    const payload = JSON.stringify({
  73.        product: product.value,
  74.        count: count.value,
  75.        price: price.value
  76.      });
  77.      const httpHeaders = {
  78.        method: 'PATCH',
  79.        body: payload
  80.      }
  81.  
  82.      fetch(`${BASE_URL}${productToEdit._id}`, httpHeaders)
  83.        .then(() => {
  84.          loadAllProductsHandler();
  85.          otherDOMSelectors.addBtn.removeAttribute('disabled');
  86.          otherDOMSelectors.updateBtn.setAttribute('disabled', true);
  87.          otherDOMSelectors.form.reset();
  88.        })
  89.        .catch((err) => {
  90.          console.error(err);
  91.        })
  92.    }
  93. ************************************************************************************************
  94. // DELETE than - catch
  95. -----------------------
  96. fetch(`${BASE_URL}${id}`, {
  97.       method: 'DELETE'
  98.     })
  99. // function deleteProductHandler() {
  100. //     const id = this.parentNode.parentNode.id;
  101. //     const httpHeaders = {
  102. //       method: 'DELETE'
  103. //     };
  104.  
  105. //     fetch(`${BASE_URL}${id}`, httpHeaders)
  106. //       .then(() => loadAllProductsHandler())
  107. //       .catch((err) => {
  108. //         console.error(err);
  109. //       })
  110. //   }
  111. // DELETE async - await
  112. -----------------------
  113. await fetch(BASE_URL + id, {
  114.         method: 'DELETE'
  115.     });
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement