// Get a single resource fetch('https://jsonplaceholder.typicode.com/posts/1') .then((response) => response.json()) .then((json) => console.log(json)); // Get a list of all resources fetch('https://jsonplaceholder.typicode.com/posts') .then((response) => response.json()) .then((json) => console.log(json)) // Create a resource 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)); // Create a resource (using a variable as the request body) const requestBody = { title: 'foo', body: 'bar', userId: 1, }; fetch('https://jsonplaceholder.typicode.com/posts', { method: 'POST', body: JSON.stringify(requestBody), headers: { 'Content-type': 'application/json; charset=UTF-8', }, }) .then((response) => response.json()) .then((json) => console.log(json)); // Create a resource (using a variable for options) const fetchOptions = { method: 'POST', body: JSON.stringify({ title: 'foo', body: 'bar', userId: 1, }), headers: { 'Content-type': 'application/json; charset=UTF-8', }, }; fetch('https://jsonplaceholder.typicode.com/posts', fetchOptions) .then((response) => response.json()) .then((json) => console.log(json))
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter