Hi I need to call a REST service as part of the buildscript (Gradle) without any 3rd party plugins, how could I use Groovy to do that?
(My first attempt)
repositories {
mavenCentral()
}
dependencies {
complie "org.codehaus.groovy.modules.http-builder:http-builder:0.5.2"
}
task hello {
def http = new HTTPBuilder("http://myserver.com:8983/solr/select?q=*&wt=json")
http.auth.basic 'username', 'password'
http.request(GET, JSON ) { req ->
}
}
The easiest way to call REST from groovy without external libraries is executing CURL. Here's an example of calling Artifactory, getting JSON back and parsing it:
import groovy.json.JsonSlurper
task hello {
def p = ['curl', '-u', '"admin:password"', "\"http://localhost:8081/api/storage/libs-release-local?list&deep=1\""].execute()
def json = new JsonSlurper().parseText(p.text)
}
Can't you just do
new URL( 'http://username:password@myserver.com:8983/solr/select?q=*&wt=json' ).text
this is working guys
import java.io.*
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.EncoderRegistry
import static groovyx.net.http.Method.*
import static groovyx.net.http.ContentType.*
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.codehaus.groovy.modules.http-builder:http-builder:0.5.2'
}
}
task hello {
def http = new groovyx.net.http.HTTPBuilder("http://local.com:8983/solr/update/json")
http.request(POST, JSON ) { req ->
req.body{
}
response.success = { resp, reader ->
println "$resp.statusLine Respond rec"
}
}
}
I'm using the JsonSlurper it looks quite simple and OS independent:
import groovy.json.JsonSlurper
String url = "http://<SONAR_URL>/api/qualitygates/project_status?projectKey=first"
def json = new JsonSlurper().parseText(url.toURL().text)
print json['projectStatus']['status']