Ask a Question

Groovy Help - Validate URL

alibaba82
Super Contributor

Groovy Help - Validate URL

hello,
One of my services has a a list or URLs in the response. These URL point to images located on some server.

I want to validate that these URLs are valid and that there is actually an image located at this URL.

Can you help me with some groovy code that some validates that these are valid URL.

thanks

Ali
4 REPLIES 4
omatzura
Super Contributor

Hi Ali,

hmm.. maybe you could try

try
{
    def in = new java.net.URL( urlToImage ).openStream()
    in.close()
}
catch( Exception e )
{
    // not available!?
}


if you get an exception, that (probably) means that the URL is not available..

let me know if this works ok!?

regards,

/Ole
eviware.com
alibaba82
Super Contributor

it works just had to change the code to

def foo = new java.net.URL( urlToImage ).openStream()

I guess 'in' is some reserved word.

Thanks for you help

Ali
alibaba82
Super Contributor

I had a follow up question on this actually.

so my response looks something like this

   
             
   

   
             
   

....


would it be possible to use groovy utils or something else so that I can stores all the URL into some list and then iterate through the list to verify that each of the URL is valid.


Thanks in advance.

Ali
omatzura
Super Contributor

Hi Ali,

you can use the XmlHolder.getNodeValues( xpath ) method which will return an array of all values matching the xpath. Then use standard groovy iteration of this array for your validations..

regards!

/Ole
eviware.com
cancel
Showing results for 
Search instead for 
Did you mean: