Created
November 16, 2020 13:23
-
-
Save Trshant/7b62eec9f2ee6ed7c88d438c96121121 to your computer and use it in GitHub Desktop.
use fetch for CRUD using mockapi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CREATE add data | |
const data = { | |
"name":"Trff vgtt", | |
"email": "[email protected]", | |
"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": "[email protected]", | |
"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