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

您应该能够将闭包传递给 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 回答