# Module /http/client

Async and sync HTTP/HTTPS client with connection pooling, headers, timeouts, and streaming bodies.

```tangu
(use /http/client)
```

## Functions & symbols

### Client

`(Client [options]) -> Client`

Constructs a reusable pooled HTTP/HTTPS client bound to `*runtime*`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `options` | `Map` | yes | Optional client config {:timeout-ms :user-agent :follow-redirects} |

```tangu
(let client (http/Client {:timeout-ms 10000}))
```

### close-stream

`(close-stream stream) -> None`

Closes an open SSE `Event-Stream` and cancels its underlying HTTP response stream.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `stream` | `Event-Stream` | no | Open SSE stream handle |

```tangu
(http/close-stream stream)
```

### content-type-of

`(content-type-of msg [default]) -> (Or String None)`

Returns the `Content-Type` header from an HTTP request or response Map.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `msg` | `Map` | no | HTTP request or response Map |
| `default` | `Any` | yes | Optional default |

```tangu
(http/content-type-of resp)
```

### delete

`(delete client url [headers]) -> Map`

Performs an HTTP `DELETE` request returning a response Map {:status :headers :body}.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `client` | `Client` | no | HTTP client created with Client |
| `url` | `String` | no | Target HTTP or HTTPS URL |
| `headers` | `Map` | yes | Optional request headers map |

```tangu
(let resp (http/delete client "https://api.example.com/items"))
```

### get

`(get client url [headers]) -> Map`

Performs an HTTP `GET` request returning a response Map {:status :headers :body}.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `client` | `Client` | no | HTTP client created with Client |
| `url` | `String` | no | Target HTTP or HTTPS URL |
| `headers` | `Map` | yes | Optional request headers map |

```tangu
(let resp (http/get client "https://api.example.com/items"))
```

### get-text

`(get-text client url [headers]) -> String`

Performs a `GET` request and returns the response body decoded directly as UTF-8 text.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `client` | `Client` | no | HTTP client |
| `url` | `String` | no | Target URL |
| `headers` | `Map` | yes | Optional headers |

```tangu
(let body (http/get-text client "https://example.com"))
```

### head

`(head client url [headers]) -> Map`

Performs an HTTP `HEAD` request returning a response Map {:status :headers :body}.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `client` | `Client` | no | HTTP client created with Client |
| `url` | `String` | no | Target HTTP or HTTPS URL |
| `headers` | `Map` | yes | Optional request headers map |

```tangu
(let resp (http/head client "https://api.example.com/items"))
```

### header-of

`(header-of msg name [default]) -> (Or String None)`

Looks up an HTTP header case-insensitively on a request or response Map.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `msg` | `Map` | no | HTTP request or response Map |
| `name` | `String | KeySymbol` | no | Case-insensitive header name |
| `default` | `Any` | yes | Optional default if absent |

```tangu
(http/header-of resp :content-type)
```

### options

`(options client url [headers]) -> Map`

Performs an HTTP `OPTIONS` request returning a response Map {:status :headers :body}.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `client` | `Client` | no | HTTP client created with Client |
| `url` | `String` | no | Target HTTP or HTTPS URL |
| `headers` | `Map` | yes | Optional request headers map |

```tangu
(let resp (http/options client "https://api.example.com/items"))
```

### patch

`(patch client url body [headers]) -> Map`

Performs an HTTP `PATCH` request with `body` and returns a response Map {:status :headers :body}.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `client` | `Client` | no | HTTP client created with Client |
| `url` | `String` | no | Target HTTP or HTTPS URL |
| `body` | `String | Slab | Map` | no | Request body payload |
| `headers` | `Map` | yes | Optional request headers map |

```tangu
(let resp (http/patch client "https://api.example.com/items" payload {:content-type "application/json"}))
```

### post

`(post client url body [headers]) -> Map`

Performs an HTTP `POST` request with `body` and returns a response Map {:status :headers :body}.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `client` | `Client` | no | HTTP client created with Client |
| `url` | `String` | no | Target HTTP or HTTPS URL |
| `body` | `String | Slab | Map` | no | Request body payload |
| `headers` | `Map` | yes | Optional request headers map |

```tangu
(let resp (http/post client "https://api.example.com/items" payload {:content-type "application/json"}))
```

### put

`(put client url body [headers]) -> Map`

Performs an HTTP `PUT` request with `body` and returns a response Map {:status :headers :body}.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `client` | `Client` | no | HTTP client created with Client |
| `url` | `String` | no | Target HTTP or HTTPS URL |
| `body` | `String | Slab | Map` | no | Request body payload |
| `headers` | `Map` | yes | Optional request headers map |

```tangu
(let resp (http/put client "https://api.example.com/items" payload {:content-type "application/json"}))
```

### receive-event

`(receive-event stream) -> (Or Map None)`

Reads the next parsed Server-Sent Event `{:event :data :id :retry}`, or `()` when the stream closes.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `stream` | `Event-Stream` | no | Open SSE stream handle |

```tangu
(while (let ev (http/receive-event stream)) (say ev:data))
```

### request

`(request client req-map) -> Map`

Executes an arbitrary HTTP request described by `req-map` and returns `{:status :headers :body}`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `client` | `Client` | no | HTTP client |
| `req` | `Map` | no | Request spec {:method :url [:headers :body]} |

```tangu
(http/request client {:method "GET" :url "https://example.com"})
```

### request-sse

`(request-sse client req-map) -> Event-Stream`

Opens a Server-Sent Events (SSE) HTTP stream handle for incremental reading via `receive-event`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `client` | `Client` | no | HTTP client |
| `req` | `Map` | no | Request spec {:method :url [:headers :body]} |

```tangu
(let stream (http/request-sse client {:method "GET" :url "https://api.example.com/events"}))
```
