HttpUtil

The HttpUtil class provides a set of methods for making HTTP requests, including GET, POST, HEAD, PUT, DELETE, PATCH, and OPTIONS, allowing you to interact with web services and retrieve or send data over the internet. It offers a convenient and flexible way to work with HTTP requests in your applications.

Methods

get(url: String): Makes an HTTP GET request to the specified URL.

var getRequest = HttpUtil.get("https://example.com/api/data");

head(url: String): Performs an HTTP HEAD request to the given URL.

var headRequest = HttpUtil.head("https://example.com/api/resource");

options(url: String): Sends an HTTP OPTIONS request to the provided URL.

var optionsRequest = HttpUtil.options("https://example.com/api/info");

post(url: String): Executes an HTTP POST request to the specified URL.

var postRequest = HttpUtil.post("https://example.com/api/create");

delete(url: String): Sends an HTTP DELETE request to the specified URL.

var deleteRequest = HttpUtil.delete("https://example.com/api/remove");

patch(url: String): Performs an HTTP PATCH request to the specified URL.

var patchRequest = HttpUtil.patch("https://example.com/api/modify");

put(url: String): Makes an HTTP PUT request to the specified URL.

var putRequest = HttpUtil.put("https://example.com/api/update");

HttpResponse Class

The HttpResponse class provides methods to retrieve information about an HTTP response. It offers methods to obtain the HTTP status code, the status text, response headers, and the response body, which can be of various types, such as strings or JSON objects. This class is essential for inspecting and working with the results of HTTP requests.

Methods

getStatus(): Integer: Retrieves the HTTP status code of the response.

getStatusText(): String: Retrieves the HTTP status text of the response.

getHeaders(): any: Retrieves the response headers as a map with the same case as the server response.

getBody(): T: Retrieves the response body, which can be of various types.


HttpRequest Class

The HttpRequest class represents an HTTP request and provides methods to convert the response body to different data types. It includes functions to retrieve the response as a string, JSON object, or binary data. HttpRequest is valuable for handling the content of HTTP responses.

Methods

asString(): HttpResponse<String>: Converts the response body to a string.

asJson(): HttpResponse<JsonNode>: Parses the response body as JSON.

asBinary(): HttpResponse<InputStream>: Retrieves the response body as binary data.


GetRequest Class

The GetRequest class includes methods for adding route parameters and request headers to a GET request. It also supports basic authentication, making it a versatile tool for customizing GET requests with specific requirements.

Methods

routeParam(name: String, value: String): Adds a route parameter to the request.

header(name: String, value: String): Adds a request header.

basicAuth(username: String, password: String): Adds basic authentication credentials.


HttpRequestWithBody Class

The HttpRequestWithBody class extends the capabilities of an HTTP request by allowing you to add route parameters, request headers, and basic authentication credentials. Additionally, it enables you to set the request body content. This class is particularly useful for configuring requests with additional data and parameters.

Methods

routeParam(name: String, value: String): Adds a route parameter to the request.

header(name: String, value: String): Adds a request header.

basicAuth(username: String, password: String): Adds basic authentication credentials.

body(body: String): Sets the request body content.

field(name: String, value: String): Adds a field to a multipart request.

field(name: String, file: File): Adds a file field to a multipart request.


RequestBodyEntity Class

The RequestBodyEntity class focuses on setting the body content of an HTTP request. It's a valuable tool for customizing the content you send in an HTTP request.

Methods

body(body: String): Sets the request body content.


MultipartBody Class

The MultipartBody class is designed for handling multipart requests, which are used for sending files and form data. It offers methods to add fields and file attachments to a multipart request. This class is essential for managing complex requests that include a combination of textual and binary data, such as file uploads.

Methods

field(name: String, value: String): Adds a field to a multipart request.

field(name: String, file: File): Adds a file field to a multipart request.

Here's a complete example of a POST request with a file upload using MultipartBody:

This example sets up a multipart request to upload a file along with some form fields.


Examples

Example: GET request from Exchanges Rate API

Retrieve and process exchange rate data from an external API, enabling further analysis or use.

Example: GET request from Local API information

This script dynamically retrieves local API data, such as country and city, based on the IP address obtained from a variable or property.

Example: Call a rest API with the IP address and return the country and city as string

Example: POST request to a data publishing API

Prepares and sends a JSON payload to a data publishing API, and it checks the response status. If the status is 200, it indicates successful data publication; otherwise, it handles errors and provides the response code.

Was this helpful?