[Python] return 401 from lambda non-proxy api gateway and return headers

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    How do I return 401 to the client, based on the statusCode returned from the lambda? (non-proxy integration). How do I return the headers from the headers field? (for 401 and 200)

    return {
    "statusCode": 401,
    "headers": {"myHeader": 'abcd "efg"'}
    }


    with this cdk:

    unauthorized_integration_response = _api_gw.IntegrationResponse(
    status_code="401",
    response_parameters=response_integration_parameters,
    selection_pattern='.*statusCode:401.*',
    )
    response_parameters = {
    "method.response.header.Access-Control-Allow-Headers": True,
    "method.response.header.Access-Control-Allow-Origin": True,
    "method.response.header.Access-Control-Allow-Methods": True,
    "method.response.header.Strict-Transport-Security": True,
    "method.response.header.Content-Security-Policy": True,
    "method.response.header.X-Frame-Options": True,
    }
    unauthorized_method_response = _api_gw.MethodResponse(
    status_code="401", response_parameters=response_parameters
    )

    myresource.add_method(
    "GET",
    _api_gw.LambdaIntegration(
    lambda_func,
    proxy=False,
    request_templates=default_request_template,
    integration_responses=[
    default_integration_response,
    unauthorized_integration_response,
    ],
    ),
    method_responses=[default_method_response, unauthorized_method_response],
    )

    Continue reading...

Compartilhe esta Página