Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling Cors-Preflight not working with custom headers #203

Closed
Andreyooo opened this issue Jul 31, 2020 · 3 comments
Closed

Enabling Cors-Preflight not working with custom headers #203

Andreyooo opened this issue Jul 31, 2020 · 3 comments
Labels

Comments

@Andreyooo
Copy link

I have a simple webpage were I make a GET-Request towards my RestAPI. Without cors it's not working, when I enable cors for all routes it does work for with content-type = text/plain.
I want to use custom headers though and content-type = application/json
so I figured I need to enable cors preflight. But it does not not work if I enable it. My code is structured as follows:

app.use(cors());
app.options('*', cors());

app.use("/exampleRoute", exampleRoute);

I get a 405 - method not allowed error, which is strange since it works with content-type = text/plain.
Any idea why it's not working? I can provide more details if needed.

@ghinks
Copy link

ghinks commented Jul 31, 2020

I am not going to make promise to fix your cors issues. But is the 405 the response to the preflight request or to the subsequent request when you have content type application/json.
In most cases a small working example of the issue that can be run be the cors maintainer is helpful in these situations.

@Andreyooo
Copy link
Author

Andreyooo commented Aug 5, 2020

Thx for your answer ghinks.
Now that I looked into it, the 405 response is regarding the OPTIONS method in the preflight request.
Shouldn't app.options('*', cors()); enable a method handler for OPTIONS? I will look if I can get a small example running.

@Andreyooo
Copy link
Author

Fixed it.
I am using some custom middleware and cors needed to be applied before this middleware. Thx for the reproducing approach, it helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants