/json-api

Primary LanguageHTML

JSON-API

This is a API collection for personal use only.

Documentation

https://my-json-server.typicode.com/arifpro/json-api

All API list

Fetching data

// GET -> all data
fetch(`https://my-json-server.typicode.com/arifpro/json-api/posts`)
  .then(res => res.json())
  .then(data => console.log(data))
  .catch(err => console.log(err));

// GET -> single data by passing id
const id = 1;
fetch(`https://my-json-server.typicode.com/arifpro/json-api/posts/${id}`)
  .then(res => res.json())
  .then(data => console.log(data))
  .catch(err => console.log(err));
// POST -> create data
fetch('https://my-json-server.typicode.com/arifpro/json-api/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));
// PUT -> update data by passing updated data
fetch('https://jsonplaceholder.typicode.com/posts/1', {
  method: 'PUT',
  body: JSON.stringify({
    id: 1,
    title: 'foo',
    body: 'bar',
    userId: 1,
  }),
  headers: {
    'Content-type': 'application/json; charset=UTF-8',
  },
})
  .then((response) => response.json())
  .then((json) => console.log(json));
// PATCH -> update data by passing specific fields
fetch('https://jsonplaceholder.typicode.com/posts/1', {
  method: 'PATCH',
  body: JSON.stringify({
    title: 'foo',
  }),
  headers: {
    'Content-type': 'application/json; charset=UTF-8',
  },
})
  .then((response) => response.json())
  .then((json) => console.log(json));
// DELETE -> delete data by passing id
fetch('https://jsonplaceholder.typicode.com/posts/1', {
  method: 'DELETE',
});

Example with all APIs