Skip to content
FutureBlitz

    Install

    Import std.net.http.client

    HttpClient

    std.net.http.client

    class

    Public Class HttpClient

    Sends requests and gives their responses. A client keeps the cookies its responses set, for the requests after, and the connections it made, which those reuse; its DefaultHeaders go with each request, before the request's own, which replace those of their names. It follows up to MaxRedirects redirects, as a browser does: a POST changes to a GET after a 301, 302, or 303, and any request but a HEAD after a 303, and a redirect to another origin takes no Authorization, Cookie, or Host of the request's own. It asks for gzip, deflate, and zstd, and decompresses what comes so. It speaks HTTP/2 to an HTTPS server that agrees to it, and HTTP/1.1 otherwise. It goes through Proxy, or else the proxy the environment names, as http_proxy, https_proxy, and no_proxy do. A failure throws: a NetError for the network's or the server's, or for a URL that isn't HTTP's or HTTPS's, and a TimeoutError for a request that took longer than its Timeout, or connecting that took longer than its ConnectTimeout.

    Examples

    Local client = HttpClient()
    client.Timeout = 10.Seconds
    client.DefaultHeaders.Set("Accept", "application/json")
    Local score = Body.Json("{{\"score\": 42}}")
    Local response = Try client.Post("https://example.com/api/scores", score)
    Try response.EnsureSuccess()
    Print(response.Status, response.Text)

    std/net/http/client.fb, line 205

    • Public ReadOnly Field DefaultHeaders: Headers field #

      The headers each request sends, before its own.

    • Public Field FollowRedirects: Bool field #

      Whether a request follows redirects, or gives a redirect's response.

    • Public Field Decompress: Bool field #

      Whether a request asks for its response compressed, which it decompresses.

    • Public Field Proxy: String? field #

      The proxy each request goes through, like "http://proxy:3128", "" for none, or Null for the environment's.

    • Public New() constructor #

      A new HttpClient, with no default headers, and 30 seconds to connect.

    • @PanicsAtCaller Public Property Timeout: Duration? property #

      How long a request may take in all, after which it throws a TimeoutError, or Null for as long as it takes, as it is to start. Panics when it's set to a Duration that isn't above 0.

    • @PanicsAtCaller Public Property ConnectTimeout: Duration property #

      How long a request's connecting, TLS's handshake too, may take, 30 seconds unless it's set, after which it throws a TimeoutError. Panics when it's set to a Duration that isn't above 0.

    • @PanicsAtCaller Public Property UserAgent: String property #

      The User-Agent header a request sends unless its headers have one. Panics when it's set to what a header's value can't be, as Headers.Add does.

    • @PanicsAtCaller Public Property MaxRedirects: Int property #

      The most redirects a request follows before it throws a NetError. Panics when it's set below 0.

    • Public Method TrustOnly(roots: Certificate[]) method #

      Trusts only roots for HTTPS from then on, instead of the OS's roots, as for a server whose certificate a private authority signed.

    • Public Method TrustOsRoots() method #

      Trusts the OS's roots again, from then on.

    • Public Method ClearCookies() method #

      Forgets the cookies it keeps.

    • Public Method Get(url: String): HttpResponse Throws NetError, TimeoutError method #

      Sends a GET to url, and gives the response.

    • Public Method Head(url: String): HttpResponse Throws NetError, TimeoutError method #

      Sends a HEAD to url, and gives the response, without a body.

    • Public Method Post(url: String, body: Body): HttpResponse Throws NetError, TimeoutError method #

      Sends a POST of body to url, and gives the response.

    • Public Method Put(url: String, body: Body): HttpResponse Throws NetError, TimeoutError method #

      Sends a PUT of body to url, and gives the response.

    • Public Method Patch(url: String, body: Body): HttpResponse Throws NetError, TimeoutError method #

      Sends a PATCH of body to url, and gives the response.

    • Public Method Delete(url: String): HttpResponse Throws NetError, TimeoutError method #

      Sends a DELETE to url, and gives the response.

    • Public Method Send(method: HttpMethod, url: String, body: Body? = Null, headers: Headers? = Null): HttpResponse Throws NetError, TimeoutError method #

      A request of method to url, with body, if it has one, and headers, after the client's.

    • Public Method Download(url: String, progress: Func(Int64, Int64)? = Null): UInt8[] Throws NetError, TimeoutError method #

      The body of url's response, which progress, unless it's Null, is told of as it comes: how many bytes came, and how many the response said it has, or -1 when it didn't say, but nothing of a redirect's own body. Throws a NetError of HTTP_STATUS when the response's status isn't success.

    • Public Method Download(url: String, path: Path, progress: Func(Int64, Int64)? = Null) Throws NetError, TimeoutError, IOError method #

      Writes url's body to the file at path as it comes, atomically, as File.WriteBytesAtomic does: to a new temporary file in its folder, which is renamed over the file, onto the disk, once all of it came, so the file holds either all of it or what it held before. progress is told of it as Download's is. Throws a NetError of HTTP_STATUS when the response's status isn't success, and an IOError when the file can't be written, which a folder that isn't there throws before anything's sent; each leaves the file as it was.

    • Public Async Method GetAsync(url: String): HttpResponse Throws NetError, TimeoutError method #

      What Get does, without blocking its thread: a call gives its Task.

    • Public Async Method HeadAsync(url: String): HttpResponse Throws NetError, TimeoutError method #

      What Head does, without blocking its thread: a call gives its Task.

    • Public Async Method PostAsync(url: String, body: Body): HttpResponse Throws NetError, TimeoutError method #

      What Post does, without blocking its thread: a call gives its Task.

    • Public Async Method PutAsync(url: String, body: Body): HttpResponse Throws NetError, TimeoutError method #

      What Put does, without blocking its thread: a call gives its Task.

    • Public Async Method PatchAsync(url: String, body: Body): HttpResponse Throws NetError, TimeoutError method #

      What Patch does, without blocking its thread: a call gives its Task.

    • Public Async Method DeleteAsync(url: String): HttpResponse Throws NetError, TimeoutError method #

      What Delete does, without blocking its thread: a call gives its Task.

    • Public Async Method SendAsync(method: HttpMethod, url: String, body: Body? = Null, headers: Headers? = Null): HttpResponse Throws NetError, TimeoutError method #

      Send's Async twin, whose request the I/O thread runs while the main thread goes on.

    • Public Async Method DownloadAsync(url: String, progress: Func(Int64, Int64)? = Null): UInt8[] Throws NetError, TimeoutError method #

      Download's Async twin, whose progress is told on the main thread.

    • Public Async Method DownloadAsync(url: String, path: Path, progress: Func(Int64, Int64)? = Null) Throws NetError, TimeoutError, IOError method #

      The Async twin of Download to a file, whose progress is told on the main thread, and whose file the I/O thread writes and puts onto the disk.

    Report an issue with this page or with FutureBlitz on GitHub.