Hi,
I'm trying to get the results of a REST endpoint, but it doesn't return anything (204 No Content) when I run it in the browser using <Jira base URL>/rest/scriptrunner/latest/custom/countryQuery. Printing the JSON I'm trying to return works and it's in the right format.
Here's the expected result:
{"id":"BRA","name":"Brazil","region":{"id":"LCN","iso2code":"ZJ","value":"Latin America & Caribbean "},"incomeLevel":{"id":"UMC","iso2code":"XT","value":"Upper middle income"},"capitalCity":"Brasilia","longitude":"-47.9292","latitude":"-15.7801"}
Here's my code:
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.Method.GET
import org.apache.log4j.Level
import org.apache.log4j.Logger
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import javax.ws.rs.core.MediaType
@BaseScript CustomEndpointDelegate delegate
countryQuery(httpMethod: "GET") { MultivaluedMap queryParams ->
log = Logger.getLogger("com.acme.workflows")
log.setLevel(Level.DEBUG)
def http = new HTTPBuilder("http://api.worldbank.org")
http.request(GET) {
uri.path = "/v2/country/br"
uri.query = [format: "json"]
response.success = { resp, json ->
log.info("Success!")
def result = [
id: json[1][0]["id"],
name: json[1][0]["name"],
region: json[1][0]["region"],
incomeLevel: json[1][0]["incomeLevel"],
capitalCity: json[1][0]["capitalCity"],
longitude: json[1][0]["longitude"],
latitude: json[1][0]["latitude"]
]
log.info("\n" + new JsonBuilder(result).toString())
return Response.ok(new JsonBuilder(result).toString()).build()
}
response.'400' = { resp, json ->
log.warn('Failed: Bad Request')
if (json) {
log.warn("Message: " + json)
}
}
response.failure = { resp, json ->
if (json) {
log.warn("Message: " + json)
}
log.warn("Failed: JSON response" + json)
log.warn("Status: " + resp.getStatus())
log.warn("Status Line: " + resp.getStatusLine())
log.warn("Data: " + resp.getData())
log.warn("Params: " + resp.getParams())
}
}
log.setLevel(Level.WARN)
}
Please help, thanks.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.