Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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 |
Expand Down
14 changes: 14 additions & 0 deletions http-client.carp
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
22 changes: 16 additions & 6 deletions test/http-client.carp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
Loading