Advertisement
karlakmkj

fetch() POST request

Dec 31st, 2020
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fetch('https://api-to-call.com/endpoint', {
  2.   ////this second argument containing method and body determines that this request is a POST request and what information will be sent to the API.
  3.   method: 'POST',  
  4.   body: JSON.stringify({id: '200'})
  5. }).then(response => {
  6.   if (response.ok){
  7.     return response.json();
  8.   }
  9.   throw new Error('Request failed!');
  10. }, networkError => {
  11.   console.log(networkError.message);
  12. }).then(jsonResponse => {
  13.   return jsonResponse
  14. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement