2011-03-16 22 views
5

最新版本的Grails的REST的客戶端插件:Grails的REST客戶端插件 - 指定報頭數據

withHttp(uri: "http://foo/bar") { 
    def bodyContent = [ 
      apiKey: "somekey", 
      identifier: identity.identity, 
      activity: ac as JSON 
     ] 
    def json = post(path: 'activity', body: bodyContent) 
    if (json.stat == 'ok') { 
     wsr.success = true 
    } 
} 

我如何標題數據添加到這個請求?

回答

8

您應該能夠將封閉傳遞給post方法並在那裏設置標題。

withHttp(uri: "http://foo/bar") { 
    def bodyContent = [ 
      apiKey: "somekey", 
      identifier: identity.identity, 
      activity: ac as JSON 
     ] 
    def json = post(path: 'activity', body: bodyContent) { 
     headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4' 
    } 
    if (json.stat == 'ok') { 
     wsr.success = true 
    } 
} 

下也應努力:

.... 
.... 
def json = post(path: 'activity', 
       body: bodyContent, 
       headers:['User-Agent':'myagent']) 
.... 
.... 
+0

我敢打賭,你是對的。我會測試它,並確保。謝謝。 – Gregg 2011-03-16 20:55:16