...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
Abstract
This code snippet accesses an external REST API and requests , and thus request JSON data and return parsed JSON data through through a Groovy script written in the Groovy editor of the add-on.
Logic
- Import the HTTPBuilder and the request methods.
- Create Access the REST API by creating a new HTTP instance.
- Pass Request the JSON data passing the method, the content type, and the request configuration closure to the request method.Handle the responses and content
- Return the parsed JSON data from the response.
Snippet
Code Block | ||||
---|---|---|---|---|
| ||||
import groovyx.net.http.HTTPBuilder import static groovyx.net.http.Method.<methodName> import static groovyx.net.http.ContentType.<contentType> // initialize a new builder and give a default URL def http = new HTTPBuilder("<URL>") http.request(<methodName>,<contentType>) { req -> response.success = { resp, reader -> assert resp.status == 200 return reader } // called only for a 404 (not found) status code: response."404" = { resp -> returnlog.error ("Not found") } } |
Placeholders
Placeholder | Description | Example |
---|---|---|
<URL > | URL | http://free.currencyconverterapi.com/api/v5/convert?q=EUR_USD&compact=y |
<methodName> | Name of the request method | GET |
<contentType> | Type of the content requested for | JSON |
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
Related articles
Filter by label (Content by label) | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
...