diff --git a/README.md b/README.md index c3b7d79..5a7cb91 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ implements the `poll` interface from the | `Client.post url headers body` | HTTP POST (auto-sets Content-Length) | | `Client.put url headers body` | HTTP PUT | | `Client.del url` | HTTP DELETE | +| `Client.head url` | HTTP HEAD | | `Client.patch url headers body` | HTTP PATCH | | `Client.request verb url headers body` | Generic request | | `Client.request-with-max-redirects verb url headers body n` | Generic request with custom redirect limit | @@ -101,6 +102,7 @@ implements the `poll` interface from the | `Client.post-with-config url headers body config` | POST with request config | | `Client.put-with-config url headers body config` | PUT with request config | | `Client.del-with-config url config` | DELETE with request config | +| `Client.head-with-config url config` | HEAD with request config | | `Client.patch-with-config url headers body config` | PATCH with request config | | `Client.request-with-config verb url headers body config` | Generic request with request config | | `Client.request-stream-with-config verb url headers body config` | Streaming with request config | diff --git a/http-client.carp b/http-client.carp index de6a1af..ef29308 100644 --- a/http-client.carp +++ b/http-client.carp @@ -555,6 +555,10 @@ See `RequestConfig` for timeout and redirect details.") (doc get "performs an HTTP GET request. Returns `(Result Response String)`.") (defn get [url] (request "GET" url (the (Map String (Array String)) {}) "")) + (doc head "performs an HTTP HEAD request. Returns `(Result Response String)`. +The response body will be empty, but headers and status code are available.") + (defn head [url] (request "HEAD" url (the (Map String (Array String)) {}) "")) + (hidden body-request) (private body-request) (defn body-request [verb url headers body] @@ -592,6 +596,16 @@ See `RequestConfig` for timeout and redirect details.") "" config)) + (doc head-with-config "performs an HTTP HEAD request with the given +`RequestConfig`. Returns `(Result Response String)`. +See `RequestConfig` for timeout and redirect details.") + (defn head-with-config [url config] + (request-with-config "HEAD" + url + (the (Map String (Array String)) {}) + "" + config)) + (hidden body-request-with-config) (private body-request-with-config) (defn body-request-with-config [verb url headers body config] diff --git a/test/http-client.carp b/test/http-client.carp index 52e543f..170ae7f 100644 --- a/test/http-client.carp +++ b/test/http-client.carp @@ -80,17 +80,20 @@ "HTTPS DELETE returns 200") ; ========================================================================= - ; Custom request + ; HEAD ; ========================================================================= (assert-true test - (match (Client.request "HEAD" - "https://example.com/" - (the (Map String (Array String)) {}) - "") + (match (Client.head "https://example.com/") (Result.Success r) (Response.ok? &r) _ false) - "custom HEAD request returns 200") + "HEAD request returns 200") + + (assert-true test + (match (Client.head "https://example.com/") + (Result.Success r) (= (String.length (Response.body &r)) 0) + _ false) + "HEAD request returns empty body") ; ========================================================================= ; Redirect following @@ -201,6 +204,13 @@ _ false)) "get-with-config with default config returns 200") + (assert-true test + (let [cfg (RequestConfig.default)] + (match (Client.head-with-config "https://example.com/" &cfg) + (Result.Success r) (Response.ok? &r) + _ false)) + "head-with-config with default config returns 200") + (assert-true test (let [cfg (RequestConfig.default)] (match (Client.post-with-config "https://httpbin.org/post"