Код IT
← Каталог

curl / fetch — API-запросы — fetch — POST с JSON (браузер или Node)

Фрагмент из «curl / fetch — API-запросы»: fetch — POST с JSON (браузер или Node).

JavaScript main.js
const res = await fetch('https://jsonplaceholder.typicode.com/posts', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    title: 'hello',
    body: 'text',
    userId: 1,
  }),
});

if (!res.ok) {
  throw new Error(`HTTP ${res.status}`);
}

const created = await res.json();
console.log('Новый id:', created.id);
const res = await fetch('https://jsonplaceholder.typicode.com/posts', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    title: 'hello',
    body: 'text',
    userId: 1,
  }),
});

if (!res.ok) {
  throw new Error(`HTTP ${res.status}`);
}

const created = await res.json();
console.log('Новый id:', created.id);