API 网关 -> SQS HTTP POST MessageAttributes

API Gateway -> SQS HTTP POST MessageAttributes

我有一个 API 网关设置,它发送到触发 Lambda 的 SQS,我试图将消息属性传递给 SQS,但是当我在邮递员中到达端点时,我不断收到 400 错误请求。 . 通过 JSON POST body

发送属性的正确方法是什么

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

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

这里是API网关的配置方式

万一以后有人偶然发现这里是从 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 也