[Python] return header without quotes in aws lambda + remove header if empty

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

  1. Stack

    Stack Membro Participativo

    I'm trying to return a header from a lambda, it's returned as "<https://example.com/ab?offset=0&limit=0>; rel=\"next\"" instead of <https://example.com/ab?offset=0&limit=0>; rel="next".

    And also when I don't return the header, I still see it as "". This is my cdk:

    _api_gw.MethodResponse(status_code='200',
    response_parameters={'method.response.header.Link': True})

    integration_response = _api_gw.IntegrationResponse(
    status_code='200',
    response_parameters=response_integration_parameters,
    response_templates={
    'application/json': """
    #set($inputRoot = $input.path('$'))

    ## Map headers only if exist ##
    #set($link = $input.json('$.Link'))
    #if($link != "")
    #set($context.responseOverride.header.Link = $link)
    #end

    ## Clean JSON body (exclude headers) ##
    {
    "data": $input.json('$.data'),

    }
    """
    }


    default_request_template = {
    "application/json": '{ "method": "$context.httpMethod", "body" : "$util.escapeJavaScript("$input.json(\'$\')")", "headers": { #foreach($param in $input.params().header.keySet()) "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end #end }, "queryParams": { #foreach($param in $input.params().querystring.keySet()) "$param": "$util.escapeJavaScript($input.params().querystring.get($param))" #if($foreach.hasNext),#end #end }, "pathParams": { #foreach($param in $input.params().path.keySet()) "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end #end }}'
    }

    resource.add_method('GET',
    _api_gw.LambdaIntegration(
    my_lambda,
    proxy=False,
    request_templates=default_request_template,
    integration_responses=[
    integration_response]),
    method_responses=[
    _api_gw.MethodResponse(status_code='200',
    response_parameters={
    'method.response.header.Link': True})])

    Continue reading...

Compartilhe esta Página