# HTTP Client

### 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.

{% hint style="info" %}
For servers requiring OAuth 2.0 authentication, leverage the [Simple OAuth 2.0 Framework for Authentication](https://sofa.k43.ch) (Freeware) to obtain access tokens efficiently and securely.
{% endhint %}

### Example GET Request

```javascript
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

```java
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());
}
```
