11

I am trying to get a few messages from a queue using the HTTP API of rabbitmq.

I am following the documentation in here I have no vhost configured.

I tried the following curl command:

curl -i -u guest:guest -H "content-type:application/json" -X POST http://127.0.0.1:15672/api/queues/foo/get -d'{"count":5,"requeue":true,"encoding":"auto","truncate":50000}'

RabbitMQ then answers:

HTTP/1.1 405 Method Not Allowed
vary: origin
Server: MochiWeb/1.1 WebMachine/1.10.0 (never breaks eye contact)
Date: Thu, 20 Apr 2017 08:03:28 GMT
Content-Length: 66
Allow: HEAD, GET, PUT, DELETE, OPTIONS

{"error":"Method Not Allowed","reason":"\"Method Not Allowed\"\n"}

Can you point out my mistake? How can I get these messages?

5 Answers 5

11

you are missing the queue name:

curl -i -u guest:guest -H "content-type:application/json" -X POST http://127.0.0.1:15672/api/queues/foo/my_queue/get -d'{"count":5,"requeue":true,"encoding":"auto","truncate":50000}'

where foo is the virtual host, and my_queue is the queue name.

as result:

[
   {
      "payload_bytes":4,
      "redelivered":true,
      "exchange":"",
      "routing_key":"my_queue",
      "message_count":5,
      "properties":{
         "delivery_mode":1,
         "headers":{

         }
      },
      "payload":"test",
      "payload_encoding":"string"
   },
   {
      "payload_bytes":4,
      "redelivered":true,
      "exchange":"",
      "routing_key":"my_queue",
      "message_count":4,
      "properties":{
         "delivery_mode":1,
         "headers":{

         }
      },
      "payload":"test",
      "payload_encoding":"string"
   },
   {
      "payload_bytes":4,
      "redelivered":true,
      "exchange":"",
      "routing_key":"my_queue",
      "message_count":3,
      "properties":{
         "delivery_mode":1,
         "headers":{

         }
      },
      "payload":"test",
      "payload_encoding":"string"
   },
   {
      "payload_bytes":4,
      "redelivered":true,
      "exchange":"",
      "routing_key":"my_queue",
      "message_count":2,
      "properties":{
         "delivery_mode":1,
         "headers":{

         }
      },
      "payload":"test",
      "payload_encoding":"string"
   },
   {
      "payload_bytes":4,
      "redelivered":true,
      "exchange":"",
      "routing_key":"my_queue",
      "message_count":1,
      "properties":{
         "delivery_mode":1,
         "headers":{

         }
      },
      "payload":"test",
      "payload_encoding":"string"
   }
]

EDIT

In case you are using the default vhost:

curl -i -u guest:guest -H "content-type:application/json" -X POST http://127.0.0.1:15672/api/queues/%2f/my_queue/get -d'{"count":5,"requeue":true,"encoding":"auto","truncate":50000}'
Sign up to request clarification or add additional context in comments.

2 Comments

Can I somehow add the filter ? for example get messages filtering by headers or properties ?
Again, No it is not possible, as I said: you can consume one message at a time in FIFO way :)!
10

Note that the syntax seems to have changed in more recent releases (and the HTTP API documentation seems to be lagging behind) and instead of the requeue option the ack_mode option needs to be set, e.g. "ack_mode":"ack_requeue_true"

So the example above for current RabbitMQ versions would be:

curl -u guest:guest -i -H "content-type:application/json" -X POST http://127.0.0.1:15672/api/queues/%2F/foo/get -d'{"count":5,"ack_mode":"ack_requeue_true","encoding":"auto","truncate":50000}' 

4 Comments

It need to be {"ackmode":"ack_requeue_true"} not {"ack_mode"="ack_requeue_true"}, otherwise you'll get not json error.
Little detail: But I'd like to point out that the answer of Akos it says "ack_mode" and in Semooze reply it's "ackmode" without the underscore, for those that are blind like me. I was getting {"error":"bad_request","reason":"[{key_missing,ackmode}]"} and didn't realize for a while that there's no underscore.
provided json has a error, so this is you use -d'{"count":5,"ackmode":"ack_requeue_true","requeue": true,"encoding":"auto","truncate":50000}'
Thanks all, I fixed the JSON (hopefully).
7

I managed to solve the problem. The key:

I have no vhost configured.

RabbitMQ uses the "/" notation for the default VHOST.

enter image description here

"/" is translated to %2F in HTTP...

So the correct call is:

curl -u guest:guest -i -H "content-type:application/json" -X POST http://127.0.0.1:15672/api/queues/%2F/foo/get -d'{"count":5,"requeue":true,"encoding":"auto","truncate":50000}' 

Comments

0

It's important to note that the documentation says the following about this endpoint :

Please note that the get path in the HTTP API is intended for diagnostics etc - it does not implement reliable delivery and so should be treated as a sysadmin's tool rather than a general API for messaging.

Comments

0

If you want to get messages and save them in a file, you can use the "Get RabbitMQ messages" package for this.

Install:

pip install rabbitgetapi

Example:

$ rabbitgetapi getqueue -f <path/fileconf.yaml> -o <outputfile> -c 1000 -s =

Where (get dlq messages):
  path/fileconf.yaml = Configuration file (In this example, the server credentials)
  outputfile = File where the message will be saved  
  c = Counter
  s = Seperator

The configuration file content:

url: 
 https://f-9e14acc7-ef3c-4a2f-a829-c96a7c8c691f.mq.us-east-1.amazonaws.com 
 
user: 
 login-user
 
password: 
 the-secret-password
 
queue: 
 DLQ_QUEUE
 
count: 
 100

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.