Thursday | 21 NOV 2024
[ previous ]
[ next ]

CORS Bug

Title:
Date: 2022-04-12
Tags:  

This probably isn't a bug but just my lack of understanding. I have nginx configured to allow all cross origin requests but when I get a cors error when using fetch in the frontend. The ultimate issue is that I need to set the content type correctly and that seems to be enough to get the issue resolved.

Nginx Conf:

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
add_header 'Access-Control-Allow-Headers' '*';

Fetch:

const response = await fetch("http://192.168.7.40:8103/api", {
    method: "post",
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data),
});

Weird! But not going to worry about it right now.

A slightly different but related issue is that moving cors to the application level and removing the cors stuff from nginx also seems to work. Smh, too much magic for my liking. I'm simply muttering an incantation so I get my development going.