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

Watch a Preflight Request

When Priya’s booking page books a walk, the browser doesn’t send the POST straight away. It first sends an OPTIONS request to the same path, asking your API whether the page may send it. That’s why the API’s terminal showed an OPTIONS line and no POST.

Key points:

  • A request that a plain HTML form could send without any script, such as a GET, or a POST of form data or plain text, goes straight out. Priya’s page has a form, but its script sends the booking with fetch() and Content-Type: application/json, which no plain form can do. That content type, a method such as DELETE, or a custom header the page adds gets a preflight request first.
  • The preflight is an OPTIONS request with three headers that matter: Origin says which page is asking, Access-Control-Request-Method names the method it wants to send, and Access-Control-Request-Headers lists the headers it set that need permission.
  • Your API has no OPTIONS route, but /walks has GET and POST routes, so Express answered for you with 200 OK and an Allow header. That answer has no Access-Control-Allow-Origin header, so the browser dropped the POST and the page’s fetch() call failed with a TypeError. Your POST handler never ran, because the browser never sent the POST.
  • For the booking’s POST to go out, the preflight needs a successful answer with Access-Control-Allow-Origin naming the page’s origin and Access-Control-Allow-Headers naming Content-Type. With only the first, the browser still refuses.
  • Each request header in the preflight has a matching answer header: Access-Control-Request-Headers is answered by Access-Control-Allow-Headers, and Access-Control-Request-Method by Access-Control-Allow-Methods. POST, like GET, never needs listing in Access-Control-Allow-Methods, so this booking doesn’t need it.
  • The response to the POST itself needs Access-Control-Allow-Origin too. Without it, the walk is still booked, but the page can’t read the answer.

Here’s the booking’s preflight, with the headers that matter:

Language: HTTP
OPTIONS /walks HTTP/1.1
Origin: http://localhost:5173
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type

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 In the last lesson, booking a walk from Priya’s page left an OPTIONS line in your API’s terminal, and no POST at all. In this video, you’ll slow that booking down and see what the browser sends before the POST.

00:16 Here’s the page loading again. Its GET goes straight to the API, and the 200 OK comes straight back, but the browser keeps it from the page’s script. Now the form books a walk for Mabel.

00:30 The page’s script calls fetch with POST and a JSON body, and sets the Content-Type header to application/json. However, the POST doesn’t leave the browser.

00:40 It waits. An OPTIONS request goes out first, to the same path.

00:47 Open up the OPTIONS request. Three of its headers matter here. The Origin header says which page is asking: the one on port 5173. Access-Control-Request-Method names POST, the method the page wants to send next.

01:03 And Access-Control-Request-Headers lists the headers the page set that need permission. Here, that’s the Content-Type header. Together, they ask the API a question: may this page send you a POST with that header?

01:17 A request that asks before the real one is called a preflight request.

01:24 So why did the GET go straight out? The browser sends a request right away when a plain HTML form, with no script, could have sent it: a GET, or a POST of form data or plain text.

01:37 Priya’s page has a form, but its script sends the booking with fetch, as application/json. No plain form can do that, or use a method such as DELETE, or a custom header the page adds.

01:49 For those, the browser asks first.

01:55 Your API has no OPTIONS route. But the walks path has GET and POST routes, so Express answered for you: 200 OK, with an Allow header listing GET, HEAD and POST.

02:08 That doesn’t answer the browser’s question. There’s no Access-Control-Allow-Origin header, so nothing says the page on port 5173 may send anything.

02:19 The browser drops the waiting POST, and the Console says the preflight doesn’t pass the access control check. The page’s fetch call fails with a TypeError.

02:28 Your POST handler never ran, because the browser never sent the POST.

02:34 What would the answer need? First, Access-Control-Allow-Origin, naming the page’s origin. Say a test server answered with that header and nothing else.

02:45 Your turn: pause the video and predict whether the browser would send the POST.

02:55 It wouldn’t. The Console says Access-Control-Allow-Headers in the answer doesn’t allow the Content-Type header. The page asked about that header, and the answer never allowed it.

03:09 The answer also needs Access-Control-Allow-Headers, and it has to name the Content-Type header. With both headers, the browser sent the test server the POST, and the handler booked the walk with 201 Created.

03:23 Each question in the preflight has a matching answer. Request-Headers is answered by Allow-Headers, and Request-Method by Allow-Methods. POST, like GET, never needs listing, so this booking doesn’t need Allow-Methods.

03:40 The POST’s own response needs Access-Control-Allow-Origin too. Without it, the walk is still booked, but the page can’t read the answer.

03:51 A request that a plain form couldn’t send goes out in two steps. First, the OPTIONS preflight asks.

03:59 Then the real request goes out, but only if the answer succeeds and allows the page’s origin and everything the page asked for. Right now, your API sends none of these headers.

04:11 In the next lesson, Allow the Booking Page’s Origin, you’ll make it send them for Priya’s page and book a walk from it.

Become a Member to join the conversation.