Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Abstract

This code snippet accesses the REST API of a client from through a Groovy script written in the Groovy editor of the add-on.

Logic

  • Import the HTTPBuilder and the required request methods.
  • Initialize Create a new builder and provide a URLHTTP instance.
  • Pass the method, the content type, and the request configuration closure to the request method.
  • Handle the responses and content.

Snippet 

Code Block
languagegroovy
linenumberstrue
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.GET<methodName>
import static groovyx.net.http.ContentType.JSON<contentType>
 
// initialize a new builder and give a default URL
def http = new HTTPBuilder("<URL>")
 
http.request(GET<methodName>,JSON<contentType>) { req ->
 
  response.success = { resp, reader ->
    assert resp.status == 200
	return reader
  }
 
  // called only for a 404 (not found) status code:
  response."404" = { resp ->
    return "Not found"
  }
}

...

PlaceholderDescriptionExample
<URL>URLhttp://free.currencyconverterapi.com/api/v5/convert?q=EUR_USD&compact=y
<methodName>Name of the request methodGET
<contentType>Type of the content requested forJSON

Context

The outcome of the code snippet depends on the content type passed to the request method. You could use this code, for example, to get a specific currency conversion rate.

Use cases

Reference

...