Client

is an HTTP client that supports both HTTP and HTTPS.

Basic GET

(match (Client.get "https://example.com/")
  (Result.Success r) (println* (Response.body &r))
  (Result.Error e) (IO.errorln &e))

POST with headers

(match (Client.post "https://api.example.com/data"
         {@"Content-Type" [@"application/json"]}
         "{\"key\": 1}")
  (Result.Success r) (println* (Response.body &r))
  (Result.Error e) (IO.errorln &e))

Streaming

(match (Client.request-stream "POST" url headers body)
  (Result.Success stream)
    (do
      (while-do true
        (match (ResponseStream.poll &stream)
          (Maybe.Nothing) (break)
          (Maybe.Just chunk) (IO.print &chunk)))
      (ResponseStream.close stream))
  (Result.Error e) (IO.errorln &e))

default-max-redirects

def

Int

is the default maximum number of HTTP redirects to follow. Used by request, request-stream, and convenience methods.

del

defn

(Fn [(Ref String a)] (Result Response String))

                        (del url)
                    

performs an HTTP DELETE request. Returns (Result Response String).

del-with-config

defn

(Fn [(Ref String a), (Ref RequestConfig b)] (Result Response String))

                        (del-with-config url config)
                    

performs an HTTP DELETE request with the given RequestConfig. Returns (Result Response String). See RequestConfig for timeout and redirect details.

del-with-jar

defn

(Fn [(Ref String a), (Ref CookieJar b)] (Result Response String))

                        (del-with-jar url jar)
                    

performs an HTTP DELETE request using the given CookieJar. Returns (Result Response String).

get

defn

(Fn [(Ref String a)] (Result Response String))

                        (get url)
                    

performs an HTTP GET request. Returns (Result Response String).

get-with-config

defn

(Fn [(Ref String a), (Ref RequestConfig b)] (Result Response String))

                        (get-with-config url config)
                    

performs an HTTP GET request with the given RequestConfig. Returns (Result Response String). See RequestConfig for timeout and redirect details.

get-with-jar

defn

(Fn [(Ref String a), (Ref CookieJar b)] (Result Response String))

                        (get-with-jar url jar)
                    

performs an HTTP GET request using the given CookieJar. Returns (Result Response String).

defn

(Fn [(Ref String a)] (Result Response String))

                        (head url)
                    

performs an HTTP HEAD request. Returns (Result Response String). The response body will be empty, but headers and status code are available.

head-with-config

defn

(Fn [(Ref String a), (Ref RequestConfig b)] (Result Response String))

                        (head-with-config url config)
                    

performs an HTTP HEAD request with the given RequestConfig. Returns (Result Response String). See RequestConfig for timeout and redirect details.

head-with-jar

defn

(Fn [(Ref String a), (Ref CookieJar b)] (Result Response String))

                        (head-with-jar url jar)
                    

performs an HTTP HEAD request using the given CookieJar. Returns (Result Response String).

patch

defn

(Fn [(Ref String a), (Map String (Array String)), (Ref String b)] (Result Response String))

                        (patch url headers body)
                    

performs an HTTP PATCH request with headers and body. Returns (Result Response String).

patch-with-config

defn

(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref RequestConfig c)] (Result Response String))

                        (patch-with-config url headers body config)
                    

performs an HTTP PATCH request with the given RequestConfig. Returns (Result Response String). See RequestConfig for timeout and redirect details.

patch-with-jar

defn

(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref CookieJar c)] (Result Response String))

                        (patch-with-jar url headers body jar)
                    

performs an HTTP PATCH request using the given CookieJar. Returns (Result Response String).

post

defn

(Fn [(Ref String a), (Map String (Array String)), (Ref String b)] (Result Response String))

                        (post url headers body)
                    

performs an HTTP POST request with headers and body. Returns (Result Response String).

post-multipart

defn

(Fn [(Ref String a), (Map String (Array String)), (Ref (Array Part) b)] (Result Response String))

                        (post-multipart url headers parts)
                    

performs an HTTP POST request with a multipart/form-data body. Generates a boundary, encodes the parts, and sets the Content-Type header automatically. Any Content-Type header in headers is overwritten. Returns (Result Response String).

(Client.post-multipart "https://example.com/upload"
  {}
  &[(Multipart.text-part "field" "value")
    (Multipart.file-part "upload" "test.txt"
                         "text/plain" "file contents")])

post-multipart-with-config

defn

(Fn [(Ref String a), (Map String (Array String)), (Ref (Array Part) b), (Ref RequestConfig c)] (Result Response String))

                        (post-multipart-with-config url headers parts config)
                    

performs an HTTP POST request with a multipart/form-data body using the given RequestConfig. Returns (Result Response String). See RequestConfig for timeout and redirect details.

post-with-config

defn

(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref RequestConfig c)] (Result Response String))

                        (post-with-config url headers body config)
                    

performs an HTTP POST request with the given RequestConfig. Returns (Result Response String). See RequestConfig for timeout and redirect details.

post-with-jar

defn

(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref CookieJar c)] (Result Response String))

                        (post-with-jar url headers body jar)
                    

performs an HTTP POST request using the given CookieJar. Returns (Result Response String).

put

defn

(Fn [(Ref String a), (Map String (Array String)), (Ref String b)] (Result Response String))

                        (put url headers body)
                    

performs an HTTP PUT request with headers and body. Returns (Result Response String).

put-with-config

defn

(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref RequestConfig c)] (Result Response String))

                        (put-with-config url headers body config)
                    

performs an HTTP PUT request with the given RequestConfig. Returns (Result Response String). See RequestConfig for timeout and redirect details.

put-with-jar

defn

(Fn [(Ref String a), (Map String (Array String)), (Ref String b), (Ref CookieJar c)] (Result Response String))

                        (put-with-jar url headers body jar)
                    

performs an HTTP PUT request using the given CookieJar. Returns (Result Response String).

request

defn

(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c)] (Result Response String))

                        (request verb url headers body)
                    

sends an HTTP request to the given URL. Returns (Result Response String).

Follows up to default-max-redirects HTTP redirects automatically. For 301, 302, and 303 responses the method is changed to GET. For 307 and 308 the original method is preserved.

See request-with-max-redirects to control the redirect limit.

request-stream

defn

(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c)] (Result ResponseStream String))

                        (request-stream verb url headers body)
                    

sends an HTTP request and returns a ResponseStream for reading the response body incrementally. Follows up to default-max-redirects HTTP redirects automatically. Use ResponseStream.poll to read chunks.

The stream handles chunked transfer-encoding automatically. Check ResponseStream.status-code to verify the request succeeded before polling.

See request-stream-with-max-redirects to control the redirect limit.

request-stream-with-config

defn

(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref RequestConfig d)] (Result ResponseStream String))

                        (request-stream-with-config verb url headers body config)
                    

sends an HTTP request with the given RequestConfig and returns a ResponseStream.

(let [cfg (RequestConfig.init 5 10 10)]
  (Client.request-stream-with-config "POST" url headers body &cfg))

See RequestConfig for timeout and redirect details.

request-stream-with-jar

defn

(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref CookieJar d)] (Result ResponseStream String))

                        (request-stream-with-jar verb url headers body jar)
                    

sends an HTTP request using the given CookieJar. Matching cookies are sent automatically, and Set-Cookie response headers are stored in the jar. Returns a ResponseStream. Follows up to default-max-redirects redirects.

request-stream-with-jar-and-config

defn

(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref CookieJar d), (Ref RequestConfig e)] (Result ResponseStream String))

                        (request-stream-with-jar-and-config verb url headers body jar config)
                    

sends an HTTP request using the given CookieJar and RequestConfig. Returns a ResponseStream.

request-stream-with-max-redirects

defn

(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), Int] (Result ResponseStream String))

                        (request-stream-with-max-redirects verb url headers body max-redirects)
                    

sends an HTTP request and returns a ResponseStream, following up to max-redirects HTTP redirects.

For 301, 302, and 303 responses the method is changed to GET and the body is dropped. For 307 and 308 responses the original method and body are preserved.

Pass 0 to disable redirect following.

request-with-config

defn

(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref RequestConfig d)] (Result Response String))

                        (request-with-config verb url headers body config)
                    

sends an HTTP request with the given RequestConfig. Returns (Result Response String).

(let [cfg (RequestConfig.init 5 10 10)]
  (Client.request-with-config "GET" url headers body &cfg))

See RequestConfig for timeout and redirect details.

request-with-jar

defn

(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref CookieJar d)] (Result Response String))

                        (request-with-jar verb url headers body jar)
                    

sends an HTTP request using the given CookieJar. Matching cookies are sent automatically, and Set-Cookie response headers are stored in the jar. Returns (Result Response String). Follows up to default-max-redirects redirects.

request-with-jar-and-config

defn

(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), (Ref CookieJar d), (Ref RequestConfig e)] (Result Response String))

                        (request-with-jar-and-config verb url headers body jar config)
                    

sends an HTTP request using the given CookieJar and RequestConfig. Returns (Result Response String).

request-with-max-redirects

defn

(Fn [(Ref String a), (Ref String b), (Map String (Array String)), (Ref String c), Int] (Result Response String))

                        (request-with-max-redirects verb url headers body max-redirects)
                    

sends an HTTP request, following up to max-redirects redirects. Returns (Result Response String). Pass 0 to disable redirect following.