Import std.net.http.client
HttpClient
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
-
The headers each request sends, before its own.
-
Whether a request follows redirects, or gives a redirect's response.
-
Whether a request asks for its response compressed, which it decompresses.
-
The proxy each request goes through, like "http://proxy:3128", "" for none, or Null for the environment's.
-
A new HttpClient, with no default headers, and 30 seconds to connect.
-
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.
-
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.
-
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.
-
The most redirects a request follows before it throws a NetError. Panics when it's set below 0.
-
Trusts only
rootsfor HTTPS from then on, instead of the OS'sroots, as for a server whose certificate a private authority signed. -
Trusts the OS's roots again, from then on.
-
Forgets the cookies it keeps.
-
Sends a GET to
url, and gives the response. -
Sends a HEAD to
url, and gives the response, without a body. -
Sends a POST of
bodytourl, and gives the response. -
Sends a PUT of
bodytourl, and gives the response. -
Sends a PATCH of
bodytourl, and gives the response. -
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, TimeoutErrormethod #A request of
methodtourl, withbody, if it has one, andheaders, after the client's. -
Public Method Download(url: String, progress: Func(Int64, Int64)? = Null): UInt8[] Throws NetError, TimeoutErrormethod #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, IOErrormethod #Writes
url's body to the file atpathas 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.progressis 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. -
What Get does, without blocking its thread: a call gives its Task.
-
What Head does, without blocking its thread: a call gives its Task.
-
Public Async Method PostAsync(url: String, body: Body): HttpResponse Throws NetError, TimeoutErrormethod #What Post does, without blocking its thread: a call gives its Task.
-
Public Async Method PutAsync(url: String, body: Body): HttpResponse Throws NetError, TimeoutErrormethod #What Put does, without blocking its thread: a call gives its Task.
-
Public Async Method PatchAsync(url: String, body: Body): HttpResponse Throws NetError, TimeoutErrormethod #What Patch does, without blocking its thread: a call gives its Task.
-
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, TimeoutErrormethod #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, TimeoutErrormethod #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, IOErrormethod #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.