Skip to content
YouTurkey Tech
All web topics

CORS: requests to another origin

Intermediate

Why a page can’t read just any server’s answers, how a server allows it with Access-Control-Allow-Origin, and when the browser sends a preflight OPTIONS request first.Builds on HTTP requests and responses

OPTIONS /notes/42 HTTP/1.1Host: api.example.comOrigin: https://app.example.comAccess-Control-Request-Method: PUTAccess-Control-Request-Headers: content-type HTTP/1.1 204 No ContentAccess-Control-Allow-Origin: https://app.example.comAccess-Control-Allow-Methods: GET, PUT, DELETEAccess-Control-Allow-Headers: content-typeAccess-Control-Max-Age: 600 PUT /notes/42 HTTP/1.1Host: api.example.comOrigin: https://app.example.comContent-Type: application/json HTTP/1.1 200 OKAccess-Control-Allow-Origin: https://app.example.com
Messages

Empty

0/14Press Play, or step through with the arrows, to begin.
Step 0 of 14

Explanation

3 min read

On this page

What is CORS?

Your browser keeps you logged in to many sites at once. If any page could read any server's answers, a page you opened by accident could read your email or your bank statement through your own browser. The same-origin policy prevents that: a page may freely read answers only from its own .

CORS (Cross-Origin Resource Sharing) is how a server makes an exception. It names, in response headers, the origins whose pages may read its answers. The browser checks those headers and either hands the response to the page or blocks it.

How do simple requests and preflights work?

  • The origin. https://app.example.com and https://api.example.com are different origins, and so are http:// and https://, or ports 3000 and 4000 on localhost.
  • Simple requests. A GET, HEAD or POST with only ordinary headers and a form-like Content-Type is something any HTML form could already send. The browser sends it straight away with an Origin header, then checks Access-Control-Allow-Origin in the response.
  • Preflighted requests. Anything else, such as PUT, DELETE, a JSON Content-Type or an Authorization header, is first asked about with an OPTIONS request. The server answers with Access-Control-Allow-Methods and Access-Control-Allow-Headers, and the real request leaves only if they allow it. Access-Control-Max-Age lets the browser remember the answer.
  • Cookies. Cross-origin fetch sends no cookies unless the page asks with credentials: 'include'. The server must then answer with Access-Control-Allow-Credentials: true and name the exact origin, not *.

What do people get wrong about CORS?

Mini example

A cross-origin request that carries a login cookie. The server must name the exact origin, allow credentials, and say with Vary: Origin that the answer depends on who asks:

GET /me HTTP/1.1Host: api.example.comOrigin: https://app.example.comCookie: sid=k8Fq2xW7pL0aZ3 HTTP/1.1 200 OKAccess-Control-Allow-Origin: https://app.example.comAccess-Control-Allow-Credentials: trueVary: OriginContent-Type: application/json

Quick check

A page on http://localhost:3000 calls fetch('http://localhost:4000/notes'). Is this a cross-origin request?

Further reading

All steps as text
  1. Step 1:

    The browser loads a notes app from https://app.example.com. That scheme, host and port together are the page’s origin.

  2. Step 2:

    The app’s JavaScript fetches the notes from api.example.com, a different host and so a different origin. The browser sends the request and adds an Origin header.

  3. Step 3:

    By default a page may not read another origin’s answers: that is the same-origin policy. Here the API opts in with Access-Control-Allow-Origin, naming the page’s origin.

  4. Step 4:

    Now the app saves a note with PUT and a JSON body. An old server might never expect that from another site, so the browser asks first, with an OPTIONS preflight.

    Lines 1–2

  5. Step 5:

    The preflight carries no note. It only asks: may a page from this origin send a PUT with a content-type header?

    Lines 3–5

  6. Step 6:

    app.example.com is on the API’s list, so it says yes: this origin, these methods, this header. The 204 has no body; the headers are the answer.

    Lines 7–10

  7. Step 7:

    Access-Control-Max-Age: 600 lets the browser remember this answer for 10 minutes, so the next PUTs skip the preflight.

    Line 11

  8. Step 8:

    Only now does the real PUT leave, with the note in its body and the Origin header again.

    Lines 13–16

  9. Step 9:

    The response must name the origin again: the preflight allowed the request, and this header allows the page to read the answer.

    Lines 18–19

  10. Step 10:

    Next, the app tries to fetch data from a third-party API at api.other.example. That is another origin again, so the browser adds Origin once more.

  11. Step 11:

    The server answers normally, but without any Access-Control-Allow-Origin header. The request reached it and the data came back.

  12. Step 12:

    But the browser won’t hand it to the page: fetch rejects, and the console shows a CORS error. The block happens in the browser, after the answer arrived.

  13. Step 13:

    The fix belongs to that API, which has to add the header, or to your own server, which can call the API for you. The page alone can’t switch CORS off.

  14. Step 14:

    Pages can send requests anywhere, but read only the answers whose server names their origin. Requests a plain HTML form couldn’t send are preflighted first.