Note: Official Docs Here
CRUD:
Letter | Stands For | Verb | Use Case |
---|---|---|---|
C | Create | POST | Making new data |
R | Read | GET | Fetching pre-existing data |
U | Update | PATCH | Editing Data |
D | Destroy | DELETE | Removing Data |
Example use cases: login/register, creating new data in DB
const response = await fetch('https://url.com/', {
method: "POST",
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
// our data TO CREATE here
})
});
const data = await response.json();
Example use cases: Fetching news posts, getting account data.
const response = await fetch('https://url.com/');
const data = await response.json();
const response = await fetch(`https://url.com/me`, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
});
const data = await response.json();
Example use cases: Editing something in a Database, making changes to any data.
const response = await fetch(`https://url.com/${id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
// our NEW/UPDATED data here
})
})
const data = await response.json();
Example use cases: Removing a post, deleting a profile picture, deleting a message.
const response = await fetch(`https://url.com/${id}`,{
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
})
const data = await response.json();