2012-07-03 62 views
0

我試圖做一個HTTP的RabbitMQ API調用知道隊列如何在那裏和其他相關信息...與境界+兔MQ Groovy的Httpbuilder認證

我需要3個變量要傳遞給API

1)url:(http:// localhost:55672/api)2)用戶名/密碼:guest/guest 3)realm:「RabbitMQ Management」//我不確定這是否重要 4)path: 「/隊列」

當我使用curl語句時,它給出了積極的迴應

sudo curl -i -u guest:guest (http://localhost:55672)/api/queues HTTP/1.1 200 OK Server: MochiWeb/1.1 WebMachine/1.7 (participate in the frantic) Date: Tue, 03 Jul 2012 01:39:05 GMT Content-Type: application/json Content-Length: 6176 Cache-Control: no-cache

但使用groovy中的httpbuilder。這裏是代碼

def http = new HTTPBuilder("(http://localhost:55672/api)") 
    http.auth.basic 'guest','guest' 

    http.request(GET) { req -> 
     uri.path = '/queues' 

     response.success = { resp, reader -> 
      assert resp.statusLine.statusCode == 200 
      println "Got response: ${resp.statusLine}" 
      println "Content-Type: ${resp.headers.'Content-Type'}" 
      println reader.json 
     } 

     response.'404' = { println 'Not found' } 
    } 

我得到「找不到」作爲結果。我不包括領域,因爲我無法在httpbuilder中插入「領域」。它只附帶OAuth,但我需要使用基本身份驗證爲兔子mq HTTP API調用。

有沒有人知道如何在httpbuilder groovy中包含領域名用於基本身份驗證?有沒有其他的方法。請讓我知道!謝謝!

回答

3

這是行不通的?

def http = new HTTPBuilder('http://localhost:55672') 
http.auth.basic 'guest','guest' 
http.request(GET) { req -> 
    uri.path = '/api/queues' 
    response.success = { resp, reader -> 
     assert resp.statusLine.statusCode == 200 
     println "Got response: ${resp.statusLine}" 
     println "Content-Type: ${resp.headers.'Content-Type'}" 
     println reader.json 
    } 
    response.'404' = { println 'Not found' } 
} 

接過括號和路徑您的基本網址,加入/api到路徑

+0

太謝謝你了!這工作。 – user1313900

+0

@ user1313900那麼你應該接受答案。 –