Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Groovy script error

Pradeep A
Contributor
June 28, 2021

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

// initialize a new builder and give a default URL
def http = new HTTPBuilder("https://www.google.com/")

return http.request(GET,TEXT) { req ->

response.success = { resp, htmlText ->
assert resp.status == 200
log.debug("My response handler got response: ${resp.statusLine}")
if(htmlText){
return htmlText.getText()
}
else{
return null
}
}

// called only for a 404 (not found) status code:
response."404" = { resp ->
log.error ("Not found")
return null
}
}

IM getting variable[response]is not declared

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 28, 2021

There is nothing wrong with the script. Did you try to run it?

Sometimes, the script editor will report static type checking errors because it's unable to confirm the script is okay or not. Because groovy doesn't require objects and variables to be strongly typed, these sorts of errors can sometimes appear. They are not always something that needs to be resolved.

In this case, the httpbuilder will have defined that variable. But your code editor can't guess that ahead of time.

TAGS
AUG Leaders

Atlassian Community Events