Skip to content

Instantly share code, notes, and snippets.

@Trshant
Created November 16, 2020 13:23
Show Gist options
  • Save Trshant/7b62eec9f2ee6ed7c88d438c96121121 to your computer and use it in GitHub Desktop.
Save Trshant/7b62eec9f2ee6ed7c88d438c96121121 to your computer and use it in GitHub Desktop.
use fetch for CRUD using mockapi
// CREATE add data
const data = {
"name":"Trff vgtt",
"email": "trs.bt@gmail.com",
"phno": 2134445
};
fetch('https://<app_id>.mockapi.io/v1/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
}).then(response => response.json()).then(data=>console.log(data.id));
// READ list data
fetch('https://<app_id>.mockapi.io/v1/users', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
}).then(response => response.json()).then(data=>console.log(data));
// UPDATE change data
const data2 = {
"name":"Trff vgtt",
"email": "trs.updated@gmail.com",
"phno": 2134445
};
fetch('https://<app_id>.mockapi.io/v1/users/57', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data2),
}).then(response => response.json()).then(data=>console.log(data));
// DELETE delete data
fetch('https://<app_id>.mockapi.io/v1/users/58', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
body: "",
}).then(response => response.json()).then(data=>console.log(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment