aws-lambda - 如何在VTL模板中附加响应头

标签 aws-lambda serverless aws-appsync response-headers vtl

我的应用程序在 serverless 框架中,我使用 vtl 模板作为 lambda 解析器。我的应用程序堆栈是 AppSyncLambda on Node JS 运行时、Serverless 框架和 vtl 模板。 我正在尝试弄清楚如何从我的 lambda 向客户端添加自定义 response headers 并且非常感谢任何输入。到目前为止,请在下面找到我的代码:

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;

无服务器 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

上面的代码在 postman 中给了我结果,但我无法在响应部分看到我的自定义 header 。我想我不知道如何在响应 vtl 中包含 header 。

最佳答案

已经晚了,但我仍然需要在每个 GraphQL 操作中添加相同的响应 header 。请引用https://aws.amazon.com/about-aws/whats-new/2022/02/aws-appsync-support-custom-response-headers/https://docs.amazonaws.cn/appsync/latest/devguide/http-helpers-in-utils-http.html .

我创建了 VTL 模板并附加到 responseMappingTemplate 中的每个解析器。它对我有用。

关于aws-lambda - 如何在VTL模板中附加响应头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65540636/

相关文章:

php - 如何在 aws lambda 中将 imagick 与 php 一起使用?

java - 将值配置到用 Java 编写的 AWS Lambda 中

aws-lambda - 与多个无服务器功能共享数据库是一种好习惯吗?

html - azure 上的无服务器 PowerShell 脚本返回 html 以在浏览器中显示

swift - 如何在 graphQL 中编写 JOIN 或从多种类型获取结果 - AWS Appsync iOS

python - 在 AWS Lambda 上使用 Python 将 MS Word(.doc 和 .docx)文件转换为 HTML

node.js - 模块初始化错误 : Error at Object. fs.openSync (fs.js :646:18) at Object. fs.readFileSync (fs.js:551:33)

amazon-web-services - 将 API 网关凭证存储在从 S3 提供服务的 AWS 无服务器网站中的何处?

reactjs - AWS GraphQL : Variable 'input' has coerced Null value for NonNull type 'Input!'

amazon-web-services - 在 Cognito 或 AppSync 中存储用户属性?