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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://java-util.k43.ch/examples/http-client.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
