Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publish to RabbitMQ queue with HTTP API

Tags:

rabbitmq

Been going through the documentation (https://cdn.rawgit.com/rabbitmq/rabbitmq-management/v3.7.9/priv/www/api/index.html)

And did not find a way to publish a message to a queue (not an exchange, a queue) with the HTTP API?

Is that possible?

As much as it might make little sens in a production mindset, it still can be useful for testing purposes.

I basically want to mimic the “Publish message” interface available in the RabbitMQ administration console.

Is this possible somehow?

like image 341
ptpdlc Avatar asked Nov 17 '18 12:11

ptpdlc


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.

Is AMQP faster than HTTP?

The results showed that MQTT and AMQP are four times faster than HTTP protocol when comparing the message sent latencies.

Is RabbitMQ an API?

Overview. The RabbitMQ management plugin provides an HTTP-based API for management and monitoring of RabbitMQ nodes and clusters, along with a browser-based UI and a command line tool, rabbitmqadmin.


2 Answers

Note: your question is already answered here: link

RabbitMQ only supports publishing to exchanges. This is a core function of the product. Then, RabbitMQ uses bindings to figure out what queue(s) should receive the message.

You can read about these concepts here.

When you use "Publish message" in the administration console, it uses the default binding and default exchange. From this document:


The default exchange

In previous parts of the tutorial we knew nothing about exchanges, but still were able to send messages to queues. That was possible because we were using a default exchange, which we identify by the empty string ("").

Recall how we published a message before:

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body=message)

The exchange parameter is the name of the exchange. The empty string denotes the default or nameless exchange: messages are routed to the queue with the name specified by routing_key, if it exists.


So, in order to appear to publish directly to a queue, the management interface publishes a message to the default exchange (named amq.default or the empty string "") using the queue name as the routing key. You can see this for yourself by enabling the developer tools in your browser and watching the HTTP call made to /api/exchanges/vhost/name/publish when you publish a message to a queue.

In your case, the request will look something like this (use Chrome, and right-click the publish request and "copy as cUrl"):

curl -4vvv -u guest:guest \
    '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"}'

NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.

like image 200
Luke Bakken Avatar answered Oct 04 '22 16:10

Luke Bakken


For those interested in Intellij IDEA HTTP Client syntax with an array of ids

[
  {"id": "83d6e4dc-0478-42da-8da0-65b508530a43"},
  {"id": "08d3e147-79c4-4b91-be7c-b1cc86e21278"}
]

POST http://localhost:15672/api/exchanges/%2F/amqp.myexchange/publish
Authorization: Basic guest guest
Content-Type: application/json

{
  "vhost": "/",
  "name": "amqp.myexchange",
  "properties": {
    "delivery_mode": 2,
    "headers": {},
    "content_type": "application/json"
  },
  "routing_key": "",
  "delivery_mode": "2",
  "payload": "[{\"id\":\"83d6e4dc-0478-42da-8da0-65b508530a43\"},{\"id\":\"08d3e147-79c4-4b91-be7c-b1cc86e21278\"}]",
  "headers": {},
  "props": {
    "content_type": "application/json"
  },
  "payload_encoding": "string"
}
like image 20
T.Rex Avatar answered Oct 04 '22 16:10

T.Rex