Grails REST Client Plugin - Specify Header Data
Asked Answered
R

1

6

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?

Ratal answered 16/3, 2011 at 18:58 Comment(0)
C
8

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'])
....
....
Copier answered 16/3, 2011 at 20:53 Comment(1)
I bet you're right. I'll test it out and make sure. Thanks.Ratal

© 2022 - 2024 — McMap. All rights reserved.