Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ - Get messages from a queue using curl

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?

like image 358
oz123 Avatar asked Apr 20 '17 08:04

oz123


2 Answers

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}'
like image 107
Gabriele Santomaggio Avatar answered Sep 21 '22 17:09

Gabriele Santomaggio


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}' 
like image 23
Ákos Avatar answered Sep 22 '22 17:09

Ákos