Page cover image

⚒️HTTP Client

Get data from server the easy way.

KHTTPClient Class Overview

  • Comprehensive HTTP Method Support Fully supports all major HTTP methods, including GET, POST, PUT, PATCH, HEAD, OPTIONS, and DELETE.

  • Automatic Header Management Automatically adds essential request headers, such as Date, User-Agent, Host, and Content-Length, ensuring seamless communication with HTTP servers.

For servers requiring OAuth 2.0 authentication, leverage the Simple OAuth 2.0 Framework for Authentication (Freeware) to obtain access tokens efficiently and securely.

Example GET Request

KHTTPClient http = new KHTTPClient();

if (!http.get("https://reqbin.com/echo/get/json")) {
   KLog.error("Error: {}", http.getLastError());
} else {
   System.out.println(http.getResponseDataAsString());
}

Example POST Request with Basic Authentication

Properties props = new Properties();
props.put("Authorization", "Basic " + K.encodeBase64(userName + ':' + password);

KHTTPClient http = new KHTTPClient();

if (!http.post("https://example.com:4443", props, "HTTP body data")) {
   KLog.abort("HTTP POST failed - " + http.getLastError());
}

Last updated