简体   繁体   English

用grails接收http状态码

[英]Receiving http status code with grails

I'm trying to receive status code with url calls.(Using grails) 我正在尝试通过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
        }
    }

Trying this code snippet to receive make url calls, but I can't figure out how to pull out status code from this. 尝试使用此代码段接收make url调用,但是我不知道如何从中提取状态代码。

What I want to do is sending error messages depending on what status code I'm getting.(for example, 400 prints forbidden message of my choice) 我要做的是根据收到的状态代码发送错误消息(例如,我选择的400条禁止打印的消息)

Any suggestions for a class to receive status code of the GET calls? 对于类接收GET调用的状态代码有什么建议吗?

The resp argument to your response.failure closure has a status you can check. 您的response.failure闭包的resp参数具有可以检查的status

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

See the Response Handlers section of the httpbuilder docs for more information and examples. 有关更多信息和示例,请参见httpbuilder文档的Response Handlers部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM