chalice icon indicating copy to clipboard operation
chalice copied to clipboard

Missing CORS header from the API response body.

Open abdullah353 opened this issue 8 years ago • 2 comments

I have enabled the Cors in the api as following:

@app.route('/dataset', methods=['POST'], cors=True)
    .....

When i deployed the code it adds the OPTIONS object for us but it won't add the Access-Control-Allow-Origin in the response of the POST request, as shown in following image: screen shot 2018-01-08 at 8 08 38 am

The swagger file generated for the cors is following:

{
  "post": {
    "responses": {
      "200": {
        "description": "200 response",
        "schema": {"$ref": "#/definitions/Empty"}
        }
      }
    }
  }
}

While the expected swagger file should be following:

{
  "post": {
    "responses": {
      "200": {
        "description": "200 response",
        "schema": {"$ref": "#/definitions/Empty"},
        "headers": {"Access-Control-Allow-Origin": {"type": "string"}
        }
      }
    }
  }
}

abdullah353 avatar Jan 08 '18 03:01 abdullah353

The way with how Chalice implements CORS is it will use the proxy integration with API Gateway. So the lambda function that Chalice creates will inject the CORS headers for you into the response that it returns and those headers will simply be proxied to API Gateway. Thus, API Gateway has no models related to the CORS headers because it is just acting as a proxy.

However for OPTIONS, there is no lambda function assigned for it and so it cannot rely on the proxy integration. Instead, Chalice has to rely on API Gateway modeling to include the CORS headers for the OPTIONS method and that is why you see it in the swagger document for the OPTIONS method.

Hopefully that makes sense. Out of curiosity, are you seeing the CORS headers when you make a sample response? They should be there. Or is there a particular reason you want the headers modeled instead of being passed in via the proxy integration?

kyleknap avatar Jan 09 '18 03:01 kyleknap

@kyleknap thanks for explaining i will close the issue.

I was getting Cors error on my UI i have mentioned it over here #670 but it has nothing to do with this i am ignoring the suggested changes.

Thanks.

abdullah353 avatar Jan 09 '18 05:01 abdullah353