curl 发送 JSON 有效负载

MMT*_*MMT 0 rabbitmq rabbitmq-exchange

我只是在与 RabbitMQ 一起使用,然后尝试向它发送一个 json 有效负载。不幸的是我收到错误:

{"error":"bad_request","reason":"payload_not_string"}
Run Code Online (Sandbox Code Playgroud)

我在某处阅读了我需要使用的内容,"content_type": "application/json"但这也无济于事。

这是我要发送的正文:

{
    "properties": {
        "delivery_mode": 2,
        "content_type": "application/json"
    },
    "routing_key": "git",
    "payload": {
        "action": "created",
        "comment": {
            "url": "https://api.github.com/repos/baxterthehacker/public-repo/comments/11056394",
            "id": 11056394
        }
    },
    "payload_encoding": "string"
}
Run Code Online (Sandbox Code Playgroud)

和完整的卷曲:

curl -i -X POST \
   -H "Content-Type:application/json" \
   -H "Authorization:Basic Z3Vlc3Q6Z3Vlc3Q=" \
   -d \
'{
    "properties": {
        "delivery_mode": 2,
        "content_type": "application/json"
    },
    "routing_key": "git",
    "payload": {
        "action": "created",
        "comment": {
            "url": "https://api.github.com/repos/baxterthehacker/public-repo/comments/11056394",
            "id": 11056394
        }
    },
    "payload_encoding": "string"
}' \
 'http://localhost:8090/api/exchanges/%2f/amq.topic/publish'
Run Code Online (Sandbox Code Playgroud)

是否可以发送 json 有效负载?我正在考虑将 Github webhooks 发送到其中一个队列。

Luk*_*ken 5

RabbitMQ 团队会监控这个邮件列表,并且只是偶尔在 StackOverflow 上回答问题。


您看到的错误是正确的,您的负载不是字符串。我必须重现这一点并重新访问HTTP API文档才能清楚这一点。

您传递给payloadJSON 中的键的值更像是 JSON - 为了使其成为字符串,您必须正确转义它并像这样传递它:

$ curl -4vvv -u guest:guest -H 'Content-Type: application/json' localhost:15672/api/exchanges/%2f/amq.topic/publish --data-binary '{
    "properties": {
        "delivery_mode": 2,
        "content_type": "application/json"
    },
    "routing_key": "git",
    "payload":"{\"action\":\"created\",\"comment\":{\"url\":\"https://api.github.com/repos/baxterthehacker/public-repo/comments/11056394\",\"id\":11056394}}",
    "payload_encoding": "string"
}'
Run Code Online (Sandbox Code Playgroud)

另一种选择是对来自 GitHub 的 JSON 进行 base-64 编码并将其作为有效负载传递 - 如果这样做,您将不必转义任何内容。