开发者

Grails REST Client Plugin - Specify Header Data

开发者 https://www.devze.com 2023-02-17 11:15 出处:网络
Latest version of the REST Client plugin for grails: withHttp(uri: \"http://foo/bar\") { def bodyContent = [

Latest version of the REST Client plugin for grails:

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

How do I add header data to this request?


You should be able to pass a Closure to the post method and set headers there.

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

The following should also work:

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

精彩评论

暂无评论...
验证码 换一张
取 消