我有一个 API 网关设置,它发送到 SQS 并触发 Lambda,我试图将消息属性传递到 SQS,但是当我到达邮递员中的端点时,我不断收到 400 错误请求..什么是正确的发送方式JSON POST 主体上的属性
这是邮递员的正文(已根据此链接尝试了一些选项)
"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
您可以在 Web 控制台中进行设置。一个例子是
{ "attrName": { "DataType":"String", "StringValue":"${request.header.headerName}" } }
当收到带有标头
headerName
的请求时,将发送 SQS 消息,并将消息属性 attrName
设置为标头的值。
不知道为什么 AWS 让这变得如此令人困惑!