Fetch with POST method
In this code example, we are calling a dummy API for testing. We will create a new "posts" resource that will have the following endpoint.
http://jsonplaceholder.typicode.com/posts
fetch('https://jsonplaceholder.typicode.com/posts', { method: 'POST', body: JSON.stringify({ title: 'foo', body: 'bar', userId: 1, }), headers: { 'Content-type': 'application/json; charset=UTF-8', }, }) .then((response) => response.json()) .then((json) => console.log(json));
Code Output
Since by default the JSON placeholder API "posts" resource has 100 records, posting a new resource will create a post with id 101.
{ id: 101, title: 'foo', body: 'bar', userId: 1 }
Leave a reply