Answers for "fetch parse json"

19

fetch json post

(async () => {
  const rawResponse = await fetch('https://httpbin.org/post', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({a: 1, b: 'Textual content'})
  });
  const content = await rawResponse.json();

  console.log(content);
})();
Posted by: Guest on May-07-2020
5

fetch response json or text

const _fetch = async props => {
   const { endpoint, options } = props
   return await fetch(endpoint, options)
   .then(async res => {
      if (res.ok) {
         return await res.clone().json().catch(() => res.text())
      }
      return false
   })
   .catch(err => {
      console.error("api", "_fetch", "err", err)
      return false
   })
}
Posted by: Guest on June-07-2020
4

fetch suntax

fetch('http://example.com/movies.json')
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    console.log(data);
  });
Posted by: Guest on March-16-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language