HttpClientFunctions
The functions in HttpClient
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
services that allows application developers to send HTTP requests and
consume HTTP responses. You can also use the http
function in the
Http
class to make ad-hoc HTTP requests.
Functions
Function | 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
HttpClientFunctions
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 JsonFunctions
and XmlFunctions
classes for accomplishing such task.
Usage
The code snippets below show how to use some of the functions of the HttpClient
. You can find more details on
the Javadocs.
Using the http(String uri)
function:
1 2 |
|
Using the http(String uri, String body)
function:
1 2 3 |
|
JSON as request body
When sending a JSON request body using the function above, you need to encode it as a String
, including new lines and tabs. Otherwise, the
function will still send a request. However, the requested service may give an Invalid JSON Payload
error (or similar).