index
Learn how to handle errors and submit data with the fetch method
The browser's fetch
method is deliberately low-level. This means there are certain things you'll almost always need to do to make requests in a real application.
HTTP errors
fetch
is only concerned with making HTTP requests. From this perspective as long as it receives a response it was successful, even if that response says something like 500 server error
. Most of the time in your application code you want to treat non-200 status codes as errors.
Challenge
Open
workshop.html
in your editorAdd a
fetch
call to"https://echo.oliverjam.workers.dev/status/404"
(this always returns a 404)Add a
.then()
and.catch()
. Which of these runs? What does the response look like?
We need to handle HTTP responses we don't want. We can do this by checking the response.ok
property. This will be true
for successful status codes (like 200
) and false
for unsuccessful ones (like 404
or 502
).
Challenge
Edit your
.then()
to check the response'sok
propertyIf the response is not okay throw a new error with the
status
property of the responseNow does your
.catch()
run?
Submitting data
fetch
allows us to make any kind of HTTP request we like. So far we have made GET
requests, but those won't allow us to submit data to a server. To do that we'll need to configure some options by passing a second argument to fetch
. E.g.
method
: to use methods other thanGET
headers
: to send extra info about the request. e.g. if we're submitting JSON we should set the"content-type"
header to"application/json"
body
: to send information to the server. If we're sending JSON we also need toJSON.stringify
the data.
Challenge
Edit your
fetch
to send aPOST
request to"https://echo.oliverjam.workers.dev/json"
Send a JSON body containing an object with whatever properties you like
Don't forget the
"content-type"
!
User input
So far we've only hard-coded our requests. In reality they're usually triggered by a user submitting a form or clicking a button. There are several different ways we can access form data in our JavaScript.
Forms
Forms are the semantically correct element for receiving user input. We should use them even when we're using JS to handle the request (rather than relying on the native browser submission).
We can add a handler for the submit event like this:
event.preventDefault()
will stop the browser trying to send the request for you. We want to handle the request with fetch
instead.
In order to send our request we have to get hold of the values the user entered. There are a few ways we could do this.
Challenge: querySelector
querySelector
Create a form with two inputs and a submit button
Add a
"submit"
event handler to the form (don't forgetpreventDefault
)Use
querySelector
to get each input's valueUse
fetch
toPOST
the data as JSON to the same URL as beforeLog the response you get from the server
Challenge: new FormData()
new FormData()
Challenge
Edit your previous solution
Use
new FormData()
to get all the input valuesTurn the FormData into an object to submit
Workshop
Create a form with a search input and submit button
When the form is submitted request the Pokémon the user typed from
"https://pokeapi.co/api/v2/pokemon/NAME"
If the request succeeds show the Pokémon's name and sprite
If the request fails show a relevant error to the user
Stretch goals
If you have extra time try using some of the other data in the response body to show e.g. the pokémon's types or stats. Write some CSS to make it pretty!
Solution preview
Last updated