AWS Api Gateway使用消息属性连接到SQS [英] AWS Api Gateway connect to SQS with message attributes

查看:288
本文介绍了AWS Api Gateway使用消息属性连接到SQS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过遵循本教程,我已经连接了Api Gateway方法以推送到SQS队列.

I have connected an Api Gateway method to push onto a SQS Queue by following this tutorial.

https://dzone.com/articles/creating -aws-service-proxy-for-amazon-sqs

这一切都很好,但是我想使用一些路径参数将一些消息属性添加到我的消息中,但是我无法使其正常工作.

This all works fine but I would like to add some Message Attributes to my messages using some path parameters but I can not get it to work.

这是本教程中的当前映射模板.

This is the current Mapping Template from the tutorial.

Action=SendMessage&MessageBody=$util.urlEncode($util.escapeJavaScript($input.json('$')))

我试图保留'application/x-www-form-urlencoded'格式,并通过将MessageAttributes更改为(现在使用静态值)来添加MessageAttributes

I've tried to keep the 'application/x-www-form-urlencoded' format and add the MessageAttributes to this by changing it to (using a static value for now)

    Action=SendMessage&MessageBody=$util.urlEncode($util.escapeJavaScript($input.json('$')))
&MessageAttribute[0][Name]=foo&MessageAttribute[0][Type]=String&MessageAttribute[0][Value]=bar

但是我得到了这个错误

{
  "Error": {
    "Code": "MalformedQueryString",
    "Message": "Keys may not contain [",
    "Type": "Sender"
  },
  "RequestId": "ea121e6e-ca37-5d14-b92b-4a2c6fedf403"
}

如何在不使用'['

推荐答案

并没有完全清楚,实际上似乎与该服务真正期望的内容有些矛盾.在线路上,它使用.member.N.表示法,并且N从1开始而不是从0开始.另外,MessageAttributes是复数.

The SQS API Reference doesn't make this entirely clear and, in fact, seems to be somewhat inconsistent with what the service really expects to see. On the wire, it uses the .member.N. notation, and N is 1-based rather than 0-based. Also, MessageAttributes is plural.

期望的格式如下:

&MessageAttributes.member.1.Name=source_ip##
&MessageAttributes.member.1.Value.DataType=String##
&MessageAttributes.member.1.Value.StringValue=$util.urlEncode($context.identity.sourceIp)##
&MessageAttributes.member.2.Name=user_agent##
&MessageAttributes.member.2.Value.DataType=String##
&MessageAttributes.member.2.Value.StringValue=$util.urlEncode($context.identity.userAgent)##
&MessageAttributes.member.3.Name=stage##
&MessageAttributes.member.3.Value.DataType=String##
&MessageAttributes.member.3.Value.StringValue=$util.urlEncode($context.stage)##

##用于提高可读性.构建Web表单时,映射模板中不能包含换行符,因为换行符最终会以表单本身结尾,并且##告诉VTL,从此处到行尾的所有内容都将被忽略,在行尾包括换行符.否则,嵌入的换行符会使表单构造不正确.

The ## are for readability. You can't have newlines in a mapping template when you're building a web form, because they would end up in the form itself, and ## tells VTL that everything from here to the end of the line is to be disregarded, including the newline at the end of the line. Embedded newlines would otherwise make the form construction incorrect.

还请注意,API不允许将这些值留空,因此,如果有可能不存在任何属性,则您将需要额外的逻辑来进行测试并将其替换为其他属性,因为索引号(iirc)必须是连续的.

Note also that the API does not allow these values to be blank, so if there is a chance that any attribute won't be present, you'll need extra logic to test this and replace the attribute with something else, because the index numbers (iirc) must be consecutive.

为什么我们使用主体映射模板为API Gateway + SQS构建Web表单,以及有关如何配置它的更多信息,请参见我对

Why we're using a body mapping template to build a web form for API Gateway + SQS, and more information on how to configure it, is also explained in more detail in my answer to Is it possible to POST to the SQS URL using the POST body?

这篇关于AWS Api Gateway使用消息属性连接到SQS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆