curl发送JSON有效负载

时间:2018-02-01 12:21:43

标签: rabbitmq rabbitmq-exchange

我只是在使用RabbitMQ,我尝试向其发送json有效负载。不幸的是我收到了错误:

{"error":"bad_request","reason":"payload_not_string"}

我在某处读到了我需要使用"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"
}

完整的卷曲:

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'

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

1 个答案:

答案 0 :(得分:3)

RabbitMQ团队监控this mailing list,有时只回答StackOverflow上的问题。

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

您传递给JSON中的payload键的值更多是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"
}'

另一种选择是对GitHub中的JSON进行base-64编码并将其作为有效负载传递 - 如果你这样做,你将不会逃避任何事情。