---
title: "Code snippet to access a REST API and request JSON data"
canonical: "https://support.appfire.com/space/JMWE/461472519/Code%20snippet%20to%20access%20a%20REST%20API%20and%20request%20JSON%20data"
format: markdown
---
> Macro (aura-html)

### Abstract

<span style="color: #091e42">This code snippet is a specialization of the </span>[<span style="color: #000000">Code snippet to make an HTTP call and request data</span>](https://appfire.atlassian.net/wiki/spaces/JMWE/pages/461406951)<span style="color: #000000">. It accesses a REST API, </span><span style="color: #172b4d">and thus requests JSON data and returns the parsed data</span><span style="color: #091e42"> through a Groovy script written in the Groovy editor of the add-on.</span>

### Logic

- <span style="color: #091e42">Import the HTTPBuilder, GET request methods and the JSON content type method.</span>
- <span style="color: #091e42">Access the REST API by c</span><span style="color: #091e42">reating a new HTTP instance.</span>
- <span style="color: #091e42">Request the JSON data passing the method as GET, the content type as JSON, and the request configuration closure to the request method.</span>
- <span style="color: #091e42">Return the parsed JSON data from the response.</span>

### Snippet 

```groovy
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.JSON
 
// initialize a new builder and give a default URL
def http = new HTTPBuilder("<URL>")
 
def data = http.request(GET,JSON) { req ->
 
  response.success = { resp, jsonData ->
    assert resp.status == 200
	return jsonData
  }
 
  // called only for a 404 (not found) status code:
  response."404" = { resp ->
    log.error ("Not found")
    return null
  }
}

if (data) {
  // do something with the returned data
}
```

##### Placeholders

| **Placeholder** | **Description** | **Example** |
| --- | --- | --- |
| `<URL`> | `URL` | [http://free.currencyconverterapi.com/api/v5/convert?q=EUR_USD&compact=y](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` |

### Example

<span style="color: #333333">The outcome of the code snippet is JSON data</span><span style="color: #091e42">.</span><span style="color: #333333"> You</span><span style="color: #333333"> could use this code,</span><span style="color: #333333"> for example, to get a specific currency conversion rate.</span>

```
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.JSON
 
// initialize a new builder and give a default URL
def http = new HTTPBuilder("http://free.currencyconverterapi.com/api/v5/convert?q=EUR_USD&compact=y")
 
return http.request(GET,JSON) { req ->
 
  response.success = { resp, jsonData ->
    assert resp.status == 200
    if(jsonData){
      return jsonData.get("EUR_USD").val
    }
    else{
      log.warn("No data returned")
      return null
    }
  }
 
  // called only for a 404 (not found) status code:
  response."404" = { resp ->
    log.error ("Not found")
	return null

  }
}
```

### Reference

- [**HTTPBuilder**](https://github.com/jgritman/httpbuilder/wiki)
- [**Groovy Documentation**](http://groovy.codehaus.org/User+Guide)

### Related articles

> Macro (contentbylabel)

> Macro (details)
> 
> | **Related issues** |  |
> | --- | --- |