Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publishing to the default rabbitmq exchange using the http api

Tags:

So I am using rabbitmqs http api to do some very basic actions in rabbit. It works great in most situations but I am having an issue figuring out how to use it to publish a message to the default rabbitmq exchange. This exchange is always present, cannot be deleted and has a binding to every queue with a routing key equal to the queue name.

My problem is that this queue does not have a name, or rather, it's name is an empty string "". And the URL I have to use to publish this message with the HTTP api includes the name of the exchange.

http://localhost:15672/api/exchanges/vhost/name/publish (Source: http://hg.rabbitmq.com/rabbitmq-management/raw-file/rabbitmq_v3_3_4/priv/www/api/index.html)

The same article mentions that in order to use the default vhost which has a name of "/", you must use %2f in place of the vhost name. This makes me think there should be a similar way to represent the deafault exchange in the url.

I tried a few different things and none of them worked:

/api/exchanges/vhost//publish /api/exchanges/vhost/""/publish /api/exchanges/vhost/''/publish /api/exchanges/vhost/ /publish /api/exchanges/vhost/%00/publish 

I'm sure I can't be the only person that has run into this issue. any help would be much appreciated.

thanks, Tom

like image 860
Tom Turner Avatar asked Sep 09 '15 18:09

Tom Turner


People also ask

What is RabbitMQ HTTP API client?

RabbitMQ is a message broker that allows clients to connect over different open and standardized protocols such as AMQP, HTTP, STOMP, MQTT, MQTT over websockets and WebSockets/Web-Stomp.

What is default exchange in RabbitMQ?

The default exchange is a direct exchange with no name (empty string) pre-declared by the broker. It has one special property that makes it very useful for simple applications: every queue that is created is automatically bound to it with a routing key which is the same as the queue name.

How do I publish a RabbitMQ message?

Publishing single messages to a RabbitMQ queue can be easily done with the UI, by simply putting the message in the UI and clicking the "Publish Message" button.

How do I add Exchange to RabbitMQ?

You can use rabbitmqadmin for that, search for Declare an exchange on that link. Additionally exachages can be created "manually" using the web browser with rabbitmq management plugin, or with rabbitmq rest api.


2 Answers

This is the way to publish a message to amq.default:

http://localhost:15672/api/exchanges/%2f/amq.default/publish

with this body

{"properties":{},  "routing_key":"queue_test",  "payload":"message test ",  "payload_encoding":"string"} 

routing_key is the queue where you will publish the message.

Following an example using a chrome plug-in:

enter image description here

like image 125
Gabriele Santomaggio Avatar answered Sep 22 '22 08:09

Gabriele Santomaggio


Here is curl to publish Message:

curl -4vvv -u admin:admin \ 'localhost:15672/api/exchanges/%2F/amq.default/publish' \ -H 'Content-Type: text/plain;charset=UTF-8' \ --data-binary '{"vhost":"/","name":"amq.default","properties":{"delivery_mode":1,"headers":{}},"routing_key":"MY-QUEUE-NAME","delivery_mode":"1","payload":"TEST","headers":{},"props":{},"payload_encoding":"string"}' 

My Sample Request:

Username: admin
Password: admin
Routing Key: sample.load.work (My queue)

    curl --location --request POST 'localhost:15672/api/exchanges/%2F/amq.default/publish' \ --header 'Content-Type: text/plain;charset=UTF-8' \ --header 'Authorization: Basic YWRtaW46YWRtaW4=' \ --data-raw '{"vhost":"/","name":"amq.default","properties":{"delivery_mode":1,"headers":{}},"routing_key":"sample.load.work","delivery_mode":"1","payload":"TEST","headers":{},"props":{},"payload_encoding":"string"}' 

Postman Snippet: enter image description here

like image 23
VeKe Avatar answered Sep 24 '22 08:09

VeKe