12

I am trying to use RabbitMQ HTTP REST client to publish messages into the queue. I am using the following url and request

http://xxxx/api/exchanges/xxxx/exc.notif/publish

{
 "routing_key":"routing.key",
  "payload":{

  },
 "payload_encoding":"string",
 "properties":{
   "headers":{
     "notif_d":"TEST",
     "notif_k": ["example1", "example2"],
     "userModTime":"timestamp"
   }
 }
}

And getting back from the rabbit the following response:

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

I have just one header set:

Content-Type:application/json

I was trying to set the

"payload_encoding":"base64",

but it didn't help. I am new to rabbit any response is welcome.

shippi
  • 2,344
  • 7
  • 22
  • 28

5 Answers5

7

Try with

{
"properties": {
"content-type": "application/json"
},
"routing_key": "testKey",
"payload": "1234",
"payload_encoding": "string"
}
Himanshu Sharma
  • 254
  • 1
  • 5
  • Thanks for the quick reply. My challenge is that I would like to have JSON in the payload. Something similar "payload" : { "nos":["test"], "parameters":{"var2":"val2","var1":"val1"}, "exception":"test-excpetion" } I tried String and escapes but still no joy. – shippi Jul 04 '17 at 15:07
  • 1
    You helped me a lot. just escaped the json properly and its working fine. Cheers – shippi Jul 04 '17 at 15:36
2

Working example. We need simple to escape doublequotes. It is important that the colon is outside of the quotes, as this causes inexplicable errors.

{
    "properties": {},
    "routing_key": "q_testing",
    "payload": "{
        \"message\": \"message from terminal\"
    }",
    "payload_encoding": "string"
}
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
1

I managed to send content-type using underscore "_" instead of dash.

See here for list of valid properties. See RabbitMQ Management HTTP API for some examples.

To publish a json message using curl to rabbit exchange:

curl -i -u guest:guest -XPOST --data '{"properties":\
{"content_type":"application/json"}, \
"routing_key":"", \
"payload":"{\"foo\":\"bar\"}",\
"payload_encoding":"string"}' \
"http://localhost:15672/api/exchanges/%2f/exchange_name/publish"

content_type is written using underscore, routing_key is empty to send a message to exchange, not to particular queue.

1

To use a JSON formatted payload you have to encode it in base64 and use the "payload_encoding": "base64" attribute.

Michal
  • 11
  • 1
0

One solution is indeed setting payload_encoding to base64. While this was already answered above, here is a complete curl example:

json='{"type": "earl-grey", "strength": 7, "milk": true}'
amqp_host=amqp.improbability.cloud
amqp_username=slarti
amqp_password=bartfast
amqp_vhost=heart-of-gold
amqp_exchange=food.dispenser
amqp_queue=tea-requests
amqp_message_id=$(uuid)
amqp_delivery_mode=2
payload=$(echo -n $json | base64 -w0)
authorization=$(echo -n "${amqp_username}:${amqp_password}" | base64 -w0)

curl https://${amqp_host}/api/exchanges/${amqp_vhost}/${amqp_exchange}/publish   \
  -H "authorization: Basic ${authorization}" \
  -H 'Content-Type: application/json;charset=UTF-8' \
  --data @- <<EOF
{
  "properties": {
    "message_id":"${amqp_message_id}",
    "delivery_mode": ${amqp_delivery_mode}
  },
  "routing_key": "${amqp_queue}",
  "payload_encoding":"base64",
  "payload": "${payload}"
}
EOF

The rendering of the above with shell variables expanded:

curl https://amqp.improbability.cloud/api/exchanges/heart-of-gold/food.dispenser/publish 
  -H "authorization: Basic c2xhcnRpOmJhcnRmYXN0" \
  -H "Content-Type: application/json;charset=UTF-8" \
  --data @- <<EOF
{
  "properties": {
    "message_id":"eb28ad00-f86f-11ed-9e7f-00155d591867",
    "delivery_mode": 2
  },
  "routing_key": "tea-requests",
  "payload_encoding":"base64",
  "payload": "eyJ0eXBlIjogImVhcmwtZ3JleSIsICJzdHJlbmd0aCI6IDcsICJtaWxrIjogdHJ1ZX0="
}
EOF

Result:

=> {"routed":true}
tshalif
  • 2,277
  • 2
  • 15
  • 10