Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

aws lambda - How to attach response header in VTL template

My application is in serverless framework and I am using vtl template as a lambda resolver. My app stack is AppSync, Lambda on Node JS runtime, Serverless framework and vtl templates. I am trying to figure how I can add custom response headers from my lambda to client and really appreciate any input on the same. Please find my code below so far:

Lambda

const securityHeaders = {
      "content-type": "application/json; charset=utf-8",
      "x-content-type-options": "nosniff",
      "cache-control": "no-store, no-cache, must-revalidate, proxy-revalidate",
    };
    
    callback(null, {
      statusCode: 200,
      headers: securityHeaders,
      body: JSON.stringify({
        data,
      })
    });

  return;

serverless yml

functions:
  getData:
    handler: src/handler.getData
    events:
      - http:
          path: getData
          method: post    
custom:
  configValidationMode: off
  appSync:
    schema: ['graphql-schemas/data.graphql']
    authenticationType: AMAZON_COGNITO_USER_POOLS
    mappingTemplates:
      - dataSource: GetData
        type: Query
        field: getData
        request: "data-request.vtl"
        response: "data-response.vtl"

data-response.vtl

## return the body
#if($ctx.result.statusCode == 200)
    ##if response is 200
    $ctx.result.body
#else
    ##if response is not 200, append the response to error block.
    $utils.appendError($ctx.result.body, "$ctx.result.statusCode")
#end

The above code giving me the result in the postman but I am not able to see my custom headers in the response section. I think I am missing on how to include headers in the response vtl.

question from:https://stackoverflow.com/questions/65540636/how-to-attach-response-header-in-vtl-template

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...