Martini REST Functions
The Rest
class in Martini is specifically designed to assist in calling RESTful API endpoints from Groovy scripts. This documentation will guide you on how to use these functions effectively in a Groovy context.
Prerequisites
- Familiarity with Groovy scripting.
- Basic understanding of REST API concepts.
- Access to Martini development environment.
Using REST Functions in Groovy
The REST functions in Martini are intended for Groovy scripts. They are not available in the functions node for use in Martini services but can be utilized directly within Groovy scripts.
Making HTTP Requests
GET Request Example
To make a GET request and process the response in JSON format:
-
Define the Endpoint URL:
1
String endpointUrl = 'https://yourapi.com/api/endpoint'
-
Perform the GET Request: You can use either the
jsonGet
function or therequest
method.-
Using
jsonGet
:1 2 3
def jsonResponse = endpointUrl.jsonGet() { response, reader -> return reader }
-
Using
request
method:1
def jsonResponse = endpointUrl.request('GET', ContentType.JSON) {}.getData()
-
-
Handle the Response:
1
println "Response: ${jsonResponse}"
Notes
- These functions are designed for straightforward HTTP interactions within Groovy scripts.
- For more complex HTTP requests or integration with other Martini components, consider using dedicated HTTP client services.
- Proper error handling is crucial, especially when dealing with external API calls.