Advertisement
karlakmkj

async POST request

Dec 31st, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const getData = async () => {
  2.   try{
  3.     const response = await fetch('https://api-to-call.com/endpoint', {
  4.       method: 'POST',
  5.       body: JSON.stringify({id: 200})
  6.     })
  7.     if (response.ok){
  8.       const jsonResponse = await response.json();
  9.       return jsonResponse;
  10.     }
  11.     throw new Error('Request failed!');
  12.   } catch(error){
  13.     console.log(error);
  14.   }
  15. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement