【问题标题】:Grails REST Client Plugin - Specify Header DataGrails REST 客户端插件 - 指定标头数据
【发布时间】:2011-07-16 20:45:16
【问题描述】:

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

如何向此请求添加标头数据?

【问题讨论】:

    标签: rest grails grails-plugin httpbuilder


    【解决方案1】:

    您应该能够将闭包传递给 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'])
    ....
    ....
    

    【讨论】:

    • 我打赌你是对的。我会测试一下并确定。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2011-07-19
    • 2019-04-04
    • 1970-01-01
    • 2017-08-20
    • 2011-01-24
    • 1970-01-01
    • 2014-12-13
    • 2020-07-11
    相关资源
    最近更新 更多