6

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
    }
}

このリクエストにヘッダー データを追加するにはどうすればよいですか?

4

1 に答える 1

8

Closure を 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'])
....
....
于 2011-03-16T20:53:19.293 に答える