http - 用grails接收http状态码

标签 http grails get http-status-codes

我正在尝试通过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/

相关文章:

.htaccess - 如何使用 .htaccess 从 HTTPS 重定向中排除文件夹

angularjs - Angular $http 状态 (-1)

http - Golang重写http请求体

data-binding - Grails - Controller 中的日期绑定(bind)

grails - Grails的响应方法以charset = ISO-8859-1输出HTML内容

java - 如何在 grails 中查询特定对象类型的继承对象集合?

PHP MySQL GET 代码审查

JavaScript ajax 发布

javascript - 解决错误 "Request header field <name> is not allowed by Access-Control-Allow-Headers in preflight response"无法访问服务器

php - $_GET 输入参数,它是一个数组