In order to prevent OPTIONS preflight requests being sent to an Azure Function, I want to add the Access-Control-Max-Age header to the OPTIONS response so that the browser caches the response for a given time.

I tried to create an Azure Proxy Function with this proxies.json file:

{
    "proxies": {
        "AddCacheHeaderToCorsPreflightResponse": {
            "debug": true,
            "matchCondition": {
                "methods": [
                    "OPTIONS",
                    "GET"
                ],
                "route": "/api/{rest}"
            },
            "backendUri": "http://%WEBSITE_HOSTNAME%/api/{rest}",
            "responseOverrides": {
                "response.headers.Access-Control-Max-Age": "31536000"
            }
        }
    }
}

But this fails to add the response header to the OPTIONS request, but, for testing purposes, I can get GET responses to return the header. It appears that Azure doesn't allow you to add proxy functions for OPTIONS requests.

Is there a way to do this in Azure?

1 Answers

0
Joey Cai On

You need to make sure that your function.json contains OPTIONS in the methods array, like so:

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": ["get", "post", "options"]
    }
  ]
}

When you configure your httptrigger function, click Integrate and choose the OPTIONS method. Alternatively, you can remove the methods array entirely and allow all methods.