hugapi / hug

Embrace the APIs of the future. Hug aims to make developing APIs as simple as possible, but no simpler.
MIT License
6.86k stars 389 forks source link

CORS Middleware help #908

Open josh2112 opened 2 years ago

josh2112 commented 2 years ago

Please forgive the question, as I'm sure this is just a misunderstanding I have about CORS or Hug middleware...

As I understand it, a request involving CORS is actually two requests: 1) A preflight request, using the OPTIONS method, where the server tells give us permission to make the 'real' request along with some headers. 2) The actual request.

I've been having a problem with duplicate requests in one of my Hug APIs. It seems like the built-in CORS middleware is adding the appropriate headers to the OPTIONS request, but then passing it on through to the API. The client then gets the OPTIONS response and sends the real requests, which results in the same operation being performed twice in my API.

It is my understanding that a CORS middleware should entirely handle the OPTIONS request, sending a response itself and not passing it on to any other middleware or the API.

Is there a reason the Hug CORS middlware doesn't do this, and is there an easy way to accomplish it?