fetch — Talking to Your API — Concept Check
Questions on fetch — Talking to Your API. fetch is the built-in HTTP client. Its one surprising behaviour is that it does not reject on 404 or 500 — you have to check response.ok yourself.
Questions in this quiz
7 questions on fetch — Talking to Your API — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses fetch to make a GET request but receives a 500 status code. What should they do to handle this error?
- ACheck response.ok and handle the error accordingly
- BSet a timeout to automatically retry the request
- CRely on the promise rejection to catch the error
- DUse a different HTTP method to avoid the error
2.What happens if you try to parse a 500 error page as JSON without checking response.ok?
- AThe fetch promise is rejected
- BThe JSON is parsed successfully
- CThe request is automatically retried
- DIt throws an 'Unexpected token <' error
3.A developer uses fetch with a POST method but forgets to set the Content-Type header. What is the consequence?
- AThe request will default to a GET method
- BThe server may not interpret the body correctly
- CThe request will fail due to a network error
- DThe fetch promise will be rejected
4.When using fetch, under what condition does the promise reject?
- AServer returns a 500 status
- BServer returns a 404 status
- CNetwork failure or CORS block
- DResponse body is empty
5.A developer uses fetch with credentials: 'include' but doesn't receive cookies. Why might this happen?
- AThe response is not JSON
- BThe fetch promise is rejected
- CThe server doesn't support cookies
- DThe request is cross-origin and cookies are blocked
6.How can a developer cancel an ongoing fetch request in React when a component unmounts?
- ASet a timeout to cancel the request
- BUse a different HTTP method
- CRely on the promise rejection
- DUse AbortController and call abort on unmount
7.What is the default HTTP method used by fetch if none is specified?
- ADELETE
- BPUT
- CGET
- DPOST