Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a URL exists or returns 404 using Groovy Script?

Tags:

groovy

I need to check if a URL exists or returns 404 using Groovy Script. Is there any groovy api available for the same ? The corresponding java methods doesn't work with Groovy.

like image 654
vijin Avatar asked Dec 28 '25 05:12

vijin


1 Answers

You can just do:

def code = new URL('http://www.google.com/made.up').openConnection().with {
    requestMethod = 'HEAD'
    connect()
    responseCode
}

assert code == 404

Of course, if the domain doesn't exist, an Exception will be thrown

like image 81
tim_yates Avatar answered Dec 31 '25 00:12

tim_yates



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!