简体   繁体   English

如何将 JSON 文件作为 MessageBody for Amazon SQS 发布?

[英]How do I POST a JSON file as MessageBody for Amazon SQS?

I run ElasticMQ locally to emulate Amazon SQS, and I want to send a JSON file as a MessageBody.我在本地运行ElasticMQ来模拟 Amazon SQS,并且我想将 JSON 文件作为 MessageBody 发送。 Here's an example request that works:这是一个有效的示例请求:

$ curl 'http://localhost:9324/queue/foo?Action=SendMessage&MessageBody={"action":"hey"}'

<SendMessageResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/">
          <SendMessageResult>
              [...]

If I want to send a large JSON file, it would make more sense to to that as a POST, something like this:如果我想发送一个大的 JSON 文件,将其作为 POST 会更有意义,如下所示:

$ curl 'http://localhost:9324/queue/foo?Action=SendMessage' -X POST \
     -H "Content-Type: application/json" --data @./bigdata.json

There was an internal server error.

Is there a way to make this work?有没有办法使这项工作?

This is an old one now, but I've recently fell short on this and wanted to add in what I've found.这是一个旧的,但我最近在这方面做得不够,想添加我发现的内容。

If you set the Content-Type to text/plain the message succeeds, which is so counter intuitive with a json data payload.如果您将Content-Type设置为text/plain则消息会成功,这与 json 数据负载非常不直观。

$ curl -X "POST" "https://sqs.us-east-1.amazonaws.com/136525823465/rfidsqs?Action=SendMessage" -H 'Content-Type: text/plain' -d '{"testKey1": 123456,"testKey2": "5sUXJYodEUVvQwVT"}'

Reponse回应

<SendMessageResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/">
  <SendMessageResult>
    <MessageId>ba2f6498-0e6b-48cd-bd8f-027d911082b6</MessageId>
    <MD5OfMessageBody>4c8132a52a4e6f0bb5ecbe758502c69f</MD5OfMessageBody>
  </SendMessageResult>
  <ResponseMetadata>
    <RequestId>ddc02593-8757-5d0b-a780-72183ae5f517</RequestId>
  </ResponseMetadata>
</SendMessageResponse>

I don't think it's possible.我不认为这是可能的。 Based on the documentation and experimentation the only content-type it accepts is form application/x-www-form-urlencoded which means that the body can only have key=value pairs.根据文档和实验,它接受的唯一内容类型是表单application/x-www-form-urlencoded ,这意味着主体只能key=value对。 You could have to supply a value that was the actual JSON body您可能必须提供一个作为实际 JSON 正文的值

cbongiorno at wa-christianb-mbp in ~/dev/core-infra on anchore-update [+!$]
$  curl -H "content-type: application/json" -d '{"id":123}' https://sqs.us-east-1.amazonaws.com/123456789/christian-anchore-test?Action=SendMessage | xmllint --format -
<?xml version="1.0"?>
<ErrorResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/">
  <Error>
    <Type>Sender</Type>
    <Code>MissingParameter</Code>
    <Message>The request must contain the parameter MessageBody.</Message>
    <Detail/>
  </Error>
  <RequestId>9452510a-0a40-5f68-8319-05cf9ed70beb</RequestId>
</ErrorResponse>

This seems like a heinously short-sighted decision but as you can tell from the params accepted there is just 1 URL and it's used as a control signal URL.这似乎是一个极其短视的决定,但从接受的参数可以看出,只有 1 个 URL,它被用作控制信号 URL。 It's very SOAP like but in a custom/hacked crap sort-of a way.它非常像 SOAP,但以一种自定义/黑客攻击的方式进行。

I have the same struggle now.我现在也有同样的挣扎。 Depending on what you're trying to do you can create a private API gateway and hookup a lambda to remap it.根据您尝试执行的操作,您可以创建一个私有 API 网关并连接一个 lambda 来重新映射它。 But no matter how you slice it, you MUST remap it但无论你如何切片,你都必须重新映射它

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

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