Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Retry Requests Safely

In this lesson, you went down the endpoint table for the repair café’s ticket API and found out which of its requests a client can safely send again after a timeout.

Key Points

  • A timeout leaves the client unsure. Dev’s desk runs on the library hall’s Wi-Fi. When a request times out, the request may have been lost on its way in, and nothing happened. Or the server did the work, and its answer was lost on the way back. From the desk, both look the same.
  • A resent POST creates another ticket. Restart your app so it starts from the five tickets in tickets.json, then send Rosa’s radio twice. Both answers are 201 Created, each with a different id. When you list the tickets, Rosa’s name turns up twice, so there are seven, because every POST to the collection creates a new ticket.
  • A resent DELETE changes nothing more. Your app can’t answer a ticket’s own path yet, so this follows what your table promises. After the copy of Rosa’s radio is deleted, a second DELETE to its path answers 404 Not Found, because the copy is already gone. The list still holds six tickets, with one Rosa’s radio. The answer is different, but the tickets end up as they did after the first one.
  • A request is idempotent when sending it twice leaves the server as sending it once does. The answer can change, as the 404 did. The definition looks only at the state the tickets end up in.
  • A safe method like GET asks for no change, so it’s idempotent too. DELETE is idempotent, and POST to the collection isn’t. HTTP promises this for GET, PUT and DELETE, among others.
  • HTTP doesn’t promise that a PATCH is idempotent. Your table’s PATCH sets the status to a value, so putting Priya’s toaster on the bench twice leaves it on the bench either way. If a volunteer changes the ticket in between, a late PATCH undoes their change. That happens even to a PATCH sent only once, so it’s a separate problem from resending.
  • A request that asks for a step can’t be resent safely. Say another API had POST /tickets/5/next-status. If the answer is lost and the desk resends, Aiko’s bike is marked fixed while it’s still on the bench. Using POST breaks no HTTP rule. The trouble is that the request asks for a step, and as a PATCH it would be as unsafe. After a timeout, a client can’t safely resend it without knowing whether the first one landed. Your PATCH names the status, so the desk can resend it.
  • A timed-out POST isn’t resent automatically. Resending Rosa’s radio could make a copy. Dev can list the tickets and look for the radio, but a missing radio doesn’t prove the first request failed, since the server may still be saving it. Making a POST safe to resend needs the server’s help, which is beyond this course.
  • Two names you may meet. Many GraphQL APIs send requests as a POST to one path, so the method can’t say what’s safe to repeat. HATEOAS, one of REST’s own rules, has answers link to the next requests.

Here’s the endpoint table with the question this lesson asked of each row:

Method Path Success Twice = once?
GET /tickets 200 Yes
POST /tickets 201, Location No
GET /tickets/:id 200 Yes
PATCH /tickets/:id 200 Yes, it sets a value
DELETE /tickets/:id 204 Yes

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your endpoint table gives every request one answer. However, Dev’s desk runs on the library hall’s Wi-Fi, and the Wi-Fi drops. Dev adds Rosa’s radio, and the desk sends its POST request.

00:15 Nothing comes back, and the desk gives up waiting. The request has timed out. So did the ticket arrive? Maybe the request was lost on its way in, and there’s no new ticket.

00:28 Or the server saved the ticket, and its answer was lost on the way back. From the desk, both look the same. The desk could send the request again. In this lesson, you’ll find out which of your table’s requests a client can safely resend.

00:46 Try the second case in your own app. Restart it first, so it starts from the five tickets in tickets.json. Then send Rosa’s radio, and send the same request again.

00:58 Both answers are 201 Created, and each ticket has a different id. Now list the tickets. Rosa’s name turns up twice, so there are seven tickets. Every POST to the collection creates a new ticket, so a resent POST creates another one.

01:18 Your app can’t answer a ticket’s own path yet, so follow what your table promises. Dev deletes the copy with a DELETE to its own path. It’s deleted, but once more the answer is lost, so the desk sends it again.

01:33 Your turn: pause the video and predict what the second DELETE changes on the server.

01:43 Nothing. The copy is already gone, so the second DELETE answers 404 Not Found. The list still holds six tickets, with one Rosa’s radio. The answer is different, but the tickets end up as they did after the first one.

02:01 A request is idempotent when sending it twice leaves the server as sending it once does. The answer can change, as the 404 did. The definition looks only at the state the tickets end up in.

02:15 Go down your table with that question. A safe method like GET asks for no change, so it’s idempotent too. DELETE is idempotent, as you’ve seen. POST to the collection isn’t.

02:29 HTTP promises this for GET, PUT and DELETE, among others.

02:35 PATCH needs a closer look, because HTTP doesn’t promise it. Your table’s PATCH sets the status to a value. Put Priya’s toaster on the bench twice, and it’s on the bench either way.

02:48 If a volunteer changes it in between, a late PATCH undoes their change, even one sent only once.

02:57 Now critique another API, whose path moves a ticket one step along. Each POST to it moves Aiko’s bike to the next status. If the answer is lost and the desk resends, the bike is marked fixed while it’s still on the bench.

03:13 Using POST breaks no HTTP rule here. The trouble is that the request asks for one more step. As a PATCH, it would be as unsafe. After a timeout, a client can’t safely resend it without knowing whether the first one landed.

03:30 Your PATCH names the status, so the desk can resend it.

03:36 That leaves Rosa’s radio. Resending it automatically could make a copy. Dev can list the tickets and look for the radio. But a missing radio doesn’t prove the first request failed, since the server may still be saving it.

03:50 Making a POST safe to resend needs the server’s help, which is beyond this course.

03:57 Many GraphQL APIs send requests as a POST to one path, so the method can’t say what’s safe to repeat. HATEOAS, one of REST’s own rules, has answers link to the next requests.

04:12 Before a client resends a request, it needs to know whether twice ends like once.

04:18 Your table now says which requests the desk can resend. Three of its rows go to /tickets/:id. In the next lesson, Read the Ticket ID From the Path, you’ll get the server to read which ticket a request is about.

Become a Member to join the conversation.