API requests Flashcards
How to send get request to API and return json data?
fetch('http://localhost:3000/cards/1.json').then((response) => { return response.json(); }).then((data) => { console.log(data) })
How to send DELETE request?
fetch(‘http://localhost:3000/cards/1.json’, { method: ‘DELETE’}).then((response) => { return response.json(); })
How to send POST request with data?
fetch(‘http://localhost:3000/cards.json’, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’
},
body: JSON.stringify({card: {question: question, answer:
answer}}) }).then((response) => {
return response.json();
}).then((data) => {
console.log(data)
})
How to send PUT request with data?
fetch(‘http://localhost:3000/cards/1.json’, {
method: ‘PUT’,
headers: {
‘Content-Type’: ‘application/json’
},
body: JSON.stringify({card: {question: question, answer:
answer}}) }).then((response) => {
return response.json();
}).then((data) => {
console.log(data)
})