rabbitmqrabbitmq-exchange

curl send JSON payload


I'm just strating with RabbitMQ and I try to send a json payload to it. Unfortunately I'm getting error:

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

I read somewhere that I need to use "content_type": "application/json" but that has not helped either.

This is the body I'm trying to send:

{
    "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"
}

And the full curl:

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'

Is it possible to send the json payload at all? I was thinking of sending Github webhooks to one of the queues.


Solution

  • The error you see is correct, your payload is not a string. I had to reproduce this and re-visit the HTTP API docs for this to become clear.

    The value you are passing to the payload key in your JSON is more JSON - in order for it to be a string, you must escape it correctly and pass it like this:

    $ 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"
    }'
    

    The other alternative is to base-64 encode the JSON from GitHub and pass that as the payload - you won't have to escape anything if you do that.