curl 发送 JSON 负载

问题描述 投票:0回答:1

我只是使用 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 webhook 发送到其中一个队列。

rabbitmq rabbitmq-exchange
1个回答
5
投票

您看到的错误是正确的,您的有效负载不是字符串。我必须重现此内容并重新访问 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 编码并将其作为有效负载传递 - 如果这样做,您将不必转义任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.