简体   繁体   English

在 aws-sdk 中为 sqs 设置 dataType JSON

[英]Setting dataType JSON in aws-sdk for sqs

Im just trying to send a json object to sqs queue by using aws-sdk npm package.我只是想通过使用 aws-sdk npm ZEFE90A8E603A67C84BB666Z 将 json object 发送到 sqs 队列。

const sqsMessage = {
    MessageAttributes: {
        "data": {
            DataType: "String",
            StringValue: payload.data
        }

    },
    MessageBody: JSON.stringify(payload),
    QueueUrl: queueUrl

If i pass json object in data attribute it tells it expected String, if i add Json then it also throws an error about type, does anyone know a workaround or what to use to get the json? If i pass json object in data attribute it tells it expected String, if i add Json then it also throws an error about type, does anyone know a workaround or what to use to get the json?

Some useful links i've found,我发现了一些有用的链接,

1)https://blog.chrismitchellonline.com/posts/aws-sqs-message-with-attributes/ 1)https://blog.chrismitchellonline.com/posts/aws-sqs-message-with-attributes/

  1. https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-send-message.html https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-send-message.html

Would really appreciate some guidance on this非常感谢对此的一些指导

Can you share the error message that you're receiving.您能否分享您收到的错误消息。 And can you show us an example of payload that you're constructing?你能给我们展示一个你正在构建的payload的例子吗? Basically, the allowable values for dataType are found here: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_MessageAttributeValue.html基本上,可以在此处找到dataType的允许值: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_MessageAttributeValue.html

Specifically, this part: "Amazon SQS supports the following logical data types: String, Number, and Binary. For the Number data type, you must use StringValue."具体来说,这部分: “Amazon SQS 支持以下逻辑数据类型:String、Number 和 Binary。对于 Number 数据类型,您必须使用 StringValue。”

So when it comes to constructing the value for your key MessageBody , you want to use JSON.因此,在构建键MessageBody的值时,您希望使用 JSON。 So the type must be string and your value, it has to be valid JSON that's really a string and the inner quotes escaped.所以类型必须是string和你的值,它必须是有效的 JSON 这真的是一个字符串并且内引号被转义。 So for example, something like this:例如,这样的事情:

"{\"foo\": \"bar\"}"

I ran into this same issue while writing unit tests in goLang and it works.我在goLang中编写单元测试时遇到了同样的问题,它可以工作。 I know this is 11 months later, but hopefully this helps you or someone else in the future:)我知道这是 11 个月后,但希望这对您或将来的其他人有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM