xxxxxxxxxx
// Get
fetch('<your-url>')
.then((response) => response.json()).then(console.log).catch(console.warn);
// POST, PUT
data = "your data"
fetch('<your-url>', {
method: 'POST', // or 'PUT'
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
})
.then(response => response.json()).then(console.log).catch(console.warn);
xxxxxxxxxx
fetch('http://example.com/songs')
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.error(err));
xxxxxxxxxx
const data = { username: 'example' };
fetch('https://example.com/profile', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});
xxxxxxxxxx
fetch('http://example.com/movies.json')
.then((response) => {
return response.json();
})
.then((myJson) => {
console.log(myJson);
});
xxxxxxxxxx
//Obj of data to send in future like a dummyDb
const data = { username: 'example' };
//POST request with body equal on data in JSON format
fetch('https://example.com/profile', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
//Then with the data from the response in JSON...
.then((data) => {
console.log('Success:', data);
})
//Then with the error genereted...
.catch((error) => {
console.error('Error:', error);
});
//
xxxxxxxxxx
const options = {method: 'GET', headers: {Accept: 'application/json'}};
fetch('https://api.test.com/data/id', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
xxxxxxxxxx
fetch('/payment', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'url': '/payment',
"X-CSRF-Token": document.querySelector('input[name=_token]').value
},
})
xxxxxxxxxx
async function postData() {
// Define the URL of the resource you want to access
const url = "https://example.com/api/endpoint";
// Define the data you want to send in the request
const data = {
name: "John Doe",
email: "johndoe@example.com"
};
// Use the `fetch` function to make a POST request to the URL
const response = await fetch(url, {
method: "POST", // specify that you want to make a POST request
body: JSON.stringify(data), // convert the data object to a JSON string
headers: {
"Content-Type": "application/json" // set the content type of the request
}
});
// Check the status code of the response to make sure it was successful
if (response.ok) {
// If the request was successful, get the data from the response
const result = await response.json();
// Do something with the data here
console.log(result);
} else {
// If the request was not successful, handle the error here
console.error("Error:", response.statusText);
}
}
xxxxxxxxxx
// Error handling while fetching API
const url = "http://dummy.restapiexample.com/api/v1/employee/40";
fetch(url) //404 error
.then( res => {
if (res.ok) {
return res.json( );
} else {
return Promise.reject(res.status);
}
})
.then(res => console.log(res))
.catch(err => console.log('Error with message: ${err}') );
xxxxxxxxxx
const fetch = require('node-fetch');
let response = await fetch(`your url paste here`, {
headers: {
Authorization: `Token ${API_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
method: 'POST',
});
const results = await response.json();
console.log("======> :: results", results);