CORS with API Gateway HTTP

0

On API Gateway REST it's posible to handle the OPTIONS with mock responses. AWS Amplify deploy the API using it.

But how do the same on API Gateway HTTP?

I'm reading this document. https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-cors.html

And works fine for the POST and GET routes that I need to implement on lambda, but ¿I need to configure a dummy lambda over OPTIONS just to support CORS?

2개 답변
1

Yes, you need to configure a dummy lambda over OPTIONS just to support CORS when using API Gateway HTTP. This is because API Gateway HTTP **does not automatically handle OPTIONS ** requests, so you need to create a mock integration that returns the appropriate CORS headers.

profile picture
답변함 6달 전
  • If I need to handle the OPTIONS request I don't get the point of the CORS feature on API Gateway HTTP. I'm missing something? what is the benefit if I need to implement the CORS headers in any case?

  • Can someone please share the applicable code to handle OPTIONS preflight CORS error. It will be very helpful.

    Thanks in advance.

0

Why API Gateway HTTP does not handle preflight OPTIONS CORS error. It is a bug from there side.

Please provide a documented solution other wise so that we can get rid of this issue.

(Api is working from POSTMAN and other similar kind of clients tool but not working when trying to access from any java script based application like react js etc...)

With the below code, it should add some header functionality so that it can handle the CORS error. ``

Thanks.

import json


def lambda_handler(event, context):
    response = {
        "isAuthorized": False,
        "context": {
            "stringKey": "value",
            "numberKey": 1,
            "booleanKey": True,
            "arrayKey": ["value1", "value2"],
            "mapKey": {"value1": "value2"}
        }
    }

    try:
        if (event["headers"]["authorization"] == "secretToken"):
            response = {
                "isAuthorized": True,
                "context": {
                    "stringKey": "value",
                    "numberKey": 1,
                    "booleanKey": True,
                    "arrayKey": ["value1", "value2"],
                    "mapKey": {"value1": "value2"}
                }
            }
            print('allowed')
            return response
        else:
            print('denied')
            return response
    except BaseException:
        print('denied')
        return response
Sourav
답변함 2달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인