Skip to content

Home

Introduction

Development

IDEs

Services

API Explorer

Releases

Release notes

Martini Runtime

Martini Desktop

Martini Online

Feature requests

Bug reports

HttpClientMethods

The methods in HttpClientMethods are intended for use in Groovy code only and are, as the class name implies, for performing HTTP-related operations.

For Groovy only

Gloop already offers what we call HTTP client services, which are a special type of Gloop services that allows application developers to send HTTP requests and consume HTTP responses. You can also use the http method in the HttpMethods class to make ad-hoc HTTP requests.

Methods

Method Description
http(String uri) Used for sending GET requests.
http(String uri, Closure<String> body) Used for sending POST requests.
http(String uri, Map params) Used for sending GET and POST requests.
http(String uri, Map params, Map headers) Used for sending HTTP requests. You can specify the parameters and headers of your requests.
http(String uri, Map params, Map headers, byte[] body) Used for sending HTTP requests. You can specify the parameters, headers, and body of the request.
http(String uri, Map params, Map headers, Closure<String> body) Used for sending HTTP requests. You can specify the parameters, headers, and body of the request.
http(String uri, Map params, Map headers, String body) Used for sending HTTP requests. You can specify the parameters, headers, and body of the request.
http(String uri, String body) Used for sending POST requests. Request body is provided as a string.

Response as String

HttpClientMethods returns the response content as a String. Depending on the service's response return type, you may need to decode it as XML or JSON. Gloop has JsonMethods and XmlMethods classes for accomplishing such task.

Usage

The code snippets below show how to use some of the methods of the HttpClientMethods. You can find more details on the Javadocs.

Using the http(String uri) method:

1
2
// Send a GET request to the specified URL
def response = "https://anapioficeandfire.com/api/characters/583".http()

Using the http(String uri, String body) method:

1
2
3
// Sends a POST request to the specified URL 
def requestBody = "{\r\n  \"username\" : \"jdoe.awesome\",\r\n  \"email\" : \"iamjdoe@email.com\"\r\n}"
def response = "https://anapioficeandfire.com/api/characters/583".http(requestBody)

JSON as request body

When sending a JSON request body using the method above, you need to encode it as a String, including new lines and tabs. Otherwise, the method will still send a request. However, the requested service may give an Invalid JSON Payload error (or similar).