Confluence Retirement

In an effort to consolidate USGS hosted Wikis, myUSGS’ Confluence service is scheduled for retirement on January 27th, 2023. The official USGS Wiki and collaboration space is now SharePoint. Please migrate existing spaces and content to the SharePoint platform and remove it from Confluence at your earliest convenience. If you need any additional information or have any concerns about this change, please contact myusgs@usgs.gov. Thank you for your prompt attention to this matter.
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