【问题标题】:recieving sqs message body without double quotes接收不带双引号的sqs消息正文
【发布时间】:2016-08-14 04:22:32
【问题描述】:

您好,我正在使用 boto3 发送和接收 sqs 消息。 我正在发送以下 sqs 消息。 {"userid":1234,"ml_algorithm_type":1,"file_format":1,"file_path":"leu.gz"}

但在接收消息时,我收到以下字符串。 {userid:1234,ml_algorithm_type:1,file_format:1,file_path:leu.gz}

我想收到我发送的确切消息。

【问题讨论】:

  • 我想收到我发送的消息。

标签: python json amazon-sqs boto3


【解决方案1】:

以下测试函数验证在 Amazon Simple Queuing Service (SQS) 中,我们可以接收到带有 JSON 双引号的消息。

请注意,在代码中,字典似乎在键和值上有单引号。这很好,因为 json.dumps() 函数将所有键和值转换为使用 JSON 标准双引号。

test_sqs.py

import json

import boto3
from moto import mock_sqs


@mock_sqs
def test_sqs():
    sqs = boto3.resource('sqs', 'us-east-1')
    queue = sqs.create_queue(QueueName='votes')

    queue.send_message(MessageBody=json.dumps({'beer': 'tasty'}))

    messages = queue.receive_messages()
    assert len(messages)
    assert messages[0].body == '{"beer": "tasty"}'  # <- double quotes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2020-03-04
    • 2022-07-19
    • 2022-01-23
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    相关资源
    最近更新 更多