我正在尝试通过url调用接收状态代码。(使用grails)

def http = new HttpResponseDecorator(url)
    // Authentication header
    http.auth.basic(username, password)
    // Http request method. Pulls out json file
    http.request(Method.GET) {
        response.success = {
            render "Success!"
        }
        response.failure = { resp ->;
            render resp
        }
    }

尝试使用此代码段接收make url调用,但我不知道如何从中提取状态代码。

我要做的是根据收到的状态码发送错误消息(例如,我选择的400条禁止打印的消息)

对类接收GET调用的状态代码有什么建议吗?

最佳答案

resp闭包的response.failure参数具有一个status您可以检查。

response.failiure = { resp ->
    if (resp.status == 400) {
        // render forbidden message
    }
}

有关更多信息和示例,请参见httpbuilder文档的Response Handlers部分。

关于http - 用grails接收http状态码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31387009/

10-10 14:13
查看更多