Sending a persistent message in RabbitMQ via HTTP API
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
RabbitMQ is a widely used open source message broker that supports multiple messaging protocols. One of its capabilities is to send messages not only through traditional AMQP protocols but also through its HTTP API. This can be particularly useful for applications not built in languages with robust AMQP support or for systems where direct connection to RabbitMQ via standard protocols is challenging.
Basics of Messaging in RabbitMQ
RabbitMQ deals primarily with entities such as Exchanges, Queues, and Bindings. An exchange accepts messages from the producer application and routes them to one or more queues, depending on the type of exchange and the routing rules defined. The queue stores messages until they can be processed by a consumer application. The routing of the message from the exchange to the queue is determined by bindings.
To ensure the delivery of messages even in the case of network failures or server crashes, RabbitMQ allows messages to be marked as "persistent". When a message is persistent, it is stored on disk rather than just in memory, which ensures that it can survive a broker restart.
Sending Persistent Messages via HTTP API
While the AMQP protocol is commonly used for interacting with RabbitMQ, the HTTP API provides a convenient alternative. Here’s how you can send a persistent message through RabbitMQ's HTTP API:
Step 1: Set Up RabbitMQ with the HTTP Plugin
Before you start, make sure that the RabbitMQ Management Plugin (rabbitmq_management) is enabled as it provides the HTTP API:
This command also starts a web server on port 15672 by default, providing access to the management interface.
Step 2: Craft the HTTP Request
To send a message, you will need to make an HTTP POST request to the appropriate URL. The general format is:
Here, replace <host>, <port>, <vhost>, and <exchange-name> with the appropriate values.
Step 3: Sending a Persistent Message
The body of the HTTP POST request should be JSON and include routing keys, payload, and importantly, delivery options:
"delivery_mode": 2 ensures that the message is marked as persistent and saved to disk. The routing_key should match your existing routing bindings.
Step 4: Execute the Request
Using a tool like curl, you can send this as an HTTP request:
Replace user, password, and other elements in the URL with your actual values.
Summary Table
| Key Element | Value | Description |
| HTTP Method | POST | Suitable for creating or updating resources |
| URL Endpoint | /api/exchanges/<vhost>/<exchange-name>/publish | API endpoint for message publishing |
JSON properties | {"delivery_mode": 2} | Sets message persistence (2 is for persistent, 1 for non-persistent) |
JSON payload | "Hello, World!" | Actual message content |
| Authentication | Basic Authentication | Required for RabbitMQ API access |
Additional Points
- Security Considerations: Running the HTTP API exposes your RabbitMQ installation to the network. It's crucial to secure accesses, perhaps placing the server behind a firewall or using secure HTTP (HTTPS).
- Monitoring and Management: The RabbitMQ management plugin also offers a wide range of monitoring capabilities which can be very beneficial for observing the state of queues, message rates, and other metrics.
Through these steps and considerations, you can effectively send persistent messages through RabbitMQ using its HTTP API, which can be an essential tool for integration in non-AMQP environments or when dealing with various microservices architectures.

