how does the request flow
tags: learning networking
content
Note
Important: it’s the browser, not the server, enforcing CORS
scenario
- user enter
domain.com - web server of
domain.comsends javascripts - browser executes those javascripts
coffee.jswants to requestapi.domain.com- browser sees the origin of
coffee.js(domain.com) is different from what it wants to request (api.domain.com) - and then:
for a simple get request:
- browser sends the requests with headers:
Origin: https://domain.com
Access-Control-Request-Method: GET
- backend response need to include relevant headers
- otherwise browser blocks
coffee.jsfrom reading backend’s response
for a more complicated request (like POST)
- browser stops this
POSTrequest - browser performs a preflight check
- browser makes an OPTIONS request to
api.domain.comwith headers:
Origin: https://domain.com
Access-Control-Request-Method: POST
- backend of
api.domain.comsends back a response with headers:
Access-Control-Allow-Origin: https://domain.com
Access-Control-Allow-Methods: GET, POST
- otherwise browser blocks the request