【问题标题】:API Gateway -> SQS HTTP POST MessageAttributesAPI 网关 -> SQS HTTP POST 消息属性
【发布时间】:2021-11-19 00:52:03
【问题描述】:

我有一个发送到触发 Lambda 的 SQS 的 API 网关设置,我正在尝试将消息属性传递给 SQS,但是当我在邮递员中点击端点时,我不断收到 400 Bad Request.. 什么是正确的方法通过 JSON POST 正文发送属性

这是邮递员的正文(根据this link尝试了一些选项)

    "message": "Message",
   "MessageAttributes": {
    "Name": "Name",
      "Type": "String",
      "Value": "my value"
    
   }
}

这是 API 网关的配置方式

【问题讨论】:

    标签: amazon-web-services aws-api-gateway amazon-sqs


    【解决方案1】:

    以防有人在稍后偶然发现这一点,这里是从 CDK 方面工作的

    let intergation = new apiGateway.CfnIntegration(this, 'Integration', {
          apiId: props.httpApi.httpApiId,
          payloadFormatVersion: '1.0',
          integrationType: 'AWS_PROXY',
          credentialsArn: apigwRole.roleArn,
          integrationSubtype: 'SQS-SendMessage',
          requestParameters: {
            QueueUrl: sqsqueue.queueUrl,
            MessageBody: '$request.body',
            MessageAttributes: '$request.body.MessageAttributes'
          }
        })
    
        new apiGateway.CfnRoute(this, 'Route', {
          apiId: props.httpApi.httpApiId,
          routeKey: apiGateway.HttpRouteKey.with('/url/foo', apiGateway.HttpMethod.POST).key,
          target: `integrations/${intergation .ref}`
        }).addDependsOn(intergation);
    
    

    和云形成

     MessageBody: $request.body
     MessageAttributes: $request.body.MessageAttribute
    

    然后在 post man 中,POST 正文内容类型为 application/json

    {
       "message": "Message",
       "MessageAttributes": {
        "Attributes": {
          "DataType": "String",
          "StringValue": "my value"
        }
       }
    }
    

    lamba 将从事件正文中为每个记录分别注销

        Records: [
        {
         ....
          body: 'Message',
          attributes: [Object],
          messageAttributes: [Object]
      
        }
      ]
    }
    
    

    上面的 messageAttributes 对象:

        {
      Attributes: {
        stringValue: 'my value',
        stringListValues: [],
        binaryListValues: [],
        dataType: 'String'
      }
    }
    

    这也使用 AWS API Gateway v2 HTTP API

    【讨论】:

      猜你喜欢
      • 2018-05-10
      • 2014-11-10
      • 1970-01-01
      • 2019-03-23
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      • 1970-01-01
      相关资源
      最近更新 更多