Child pages
  • GET calls from Groovy
Skip to end of metadata
Go to start of metadata

Using Groovy Services to Make GET Calls

Using grails.converters.JSON we can easily make GET calls to the REST services of ScienceBase.

The Basics

Create your URL as a groovy URL with all of your fields correctly set (when using JSON make sure you set "format=json" as one of your fields). Then call .getText() on your URL object to get the returned text and JSON.parse to parse your returned text into a JSON object. The returned JSON object is basically just a Map with key/value pairs.

Example

Here is an example of search done in EERMA:

String search = "oil"
String sciencebase = "http://www.sciencebase.gov/catalog/"
List<String> fields = ["id", "title", "hasChildren", "facets", "files"]

def url = sciencebase + "items"

def fields = [
     sort: "title",
     s: "Search",
     format: "json",
     folderId: "<folderIdOfEERMA>",
     q: search,
     fields: fields.join(',')
]

def returnedJSON = JSON.parse("${url}?${getQueryString(fields)}".toURL().getText())
def items = returnedJSON.items as List

def getQueryString(Map m) {
    return m.collect {k,v->
        "${k.encodeAsURL()}=${v.encodeAsURL()}"
    }.join("&")
}
  • No labels