API requests Flashcards

1
Q

How to send get request to API and return json data?

A
fetch('http://localhost:3000/cards/1.json').then((response) => {
  return response.json();
}).then((data) => {
  console.log(data)
})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to send DELETE request?

A

fetch(‘http://localhost:3000/cards/1.json’, { method: ‘DELETE’}).then((response) => { return response.json(); })

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to send POST request with data?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to send PUT request with data?

A

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)
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly