Answers for "fetch post request"

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
1

fetch post

// data to be sent to the POST request
let _data = {
  title: "foo",
  body: "bar", 
  userId:1
}

fetch('https://jsonplaceholder.typicode.com/posts', {
  method: "POST",
  body: JSON.stringify(_data),
  headers: {"Content-type": "application/json; charset=UTF-8"}
})
.then(response => response.json()) 
.then(json => console.log(json))
.catch(err => console.log(err));
Posted by: Guest on September-06-2021
33

fetch post request

// fetch()
const url = 'http://localhost:3000/api/home';
const options = {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json;charset=UTF-8'
  },
  body: JSON.stringify({
    name: 'David',
    age: 45
  })
};

fetch(url, options)
  .then(response => {
    console.log(response.status);
  });
Posted by: Guest on October-08-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language