CORS intricacies
-
Our client needs to be whitelisted.
In other word tell your server that you want it to allow requests from a specific origin to get through.
To do this we need to add
Access-Control-Allow-Origin(ACAO) to our response which also includes the client’s origin. It is an array:Access-Control-Allow-Origin: https://website-host.domain -
For
PUT/DELETE/PATCHrequests-
Client will make a preflight request with:
OPTIONhttp method.-
Access-Control-Request-Method: PUTin request headers.Note that the value can be any other HTTP verb.
-
Server responses, and inside the response header we have:
Access-Control-Allow-Method: GET,POST,PUTheader. Note that this list can contain more HTTP verbs.
-
-
For credentials and passing cookies:
- Client will make a preflight request with:
OPTIONhttp method.- And
Access-Control-Allow-Method: PUTin request header.- Note that the value can be any other HTTP verb.
- IMPORTANT: We do not have
Access-Control-Allow-Credentialsin the request though.
- Your server’s response should have a
Access-Control-Allow-Credentialsheader.
- Client will make a preflight request with:
Here you can see a very ver very simple implementation of it in ExpressJS. But here you can see how it is done in the bigger picture.