shippi
shippi

Reputation: 2464

RabbitMQ REST HTTP JSON payload

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.

Upvotes: 12

Views: 22935

Answers (5)

tshalif
tshalif

Reputation: 2612

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}

Upvotes: 1

Michal
Michal

Reputation: 11

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

Upvotes: 1

Druta Ruslan
Druta Ruslan

Reputation: 7402

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

Upvotes: 2

Evgeny Cheryomushkin
Evgeny Cheryomushkin

Reputation: 380

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.

Upvotes: 1

Himanshu Sharma
Himanshu Sharma

Reputation: 254

Try with

{
"properties": {
"content-type": "application/json"
},
"routing_key": "testKey",
"payload": "1234",
"payload_encoding": "string"
}

Upvotes: 7

Related Questions