Import std.net.http.client
std.net.http.client
HttpClient, which sends HTTP and HTTPS requests and gives their responses, over libcurl, whose TLS is mbedTLS's: it follows redirects, decompresses what comes compressed, keeps cookies, and trusts the OS's roots unless it's given its own. A download goes to memory, or atomically to a file as it comes. Each request has an Async twin, which the I/O thread runs. Http's Static Methods send one request each, through a client of their own.
std/net/http/client.fb
Types
HttpMethod
enum
Public Enum HttpMethod
The methods a request can have.
std/net/http/client.fb, line 61
-
GET, which asks for what's at the URL.
-
HEAD, which asks for a GET's headers alone.
-
POST, which sends what the URL handles.
-
PUT, which puts what it sends at the URL.
-
PATCH, which changes part of what's at the URL.
-
DELETE, which removes what's at the URL.
-
OPTIONS, which asks what the URL allows.
Body
struct
Public Struct Body
What a request sends: its bytes, and what they are, as its Content-Type header says unless the request's headers say it themselves. Panics for a contentType a header's value can't be, as Headers.Add does.
std/net/http/client.fb, line 81
-
Its bytes.
-
Its Content-Type, or Null to send none.
-
@PanicsAtCaller Public New(bytes: UInt8[], contentType: String? = "application/octet-stream")constructor #A Body of
bytes, which are whatcontentTypesays. -
@PanicsAtCaller Public Static Method Text(text: String, contentType: String = "text/plain; charset=utf-8"): Bodystatic method #text's UTF-8, as plain text unlesscontentTypesays another kind. -
JSON's text, as application/json.
-
A form's
fields, in their order, as application/x-www-form-urlencoded: each name and value with each byte but the letters, digits, '-', '.', '_', and '~' as %XX, which servers read as they read a browser's '+' for a space, each pair as name=value, and '&' between them.
HttpResponse
class
Public Class HttpResponse
What a server answered: its status, its headers, its body's bytes, the URL it came from, after the redirects followed, and the HTTP it came over, "HTTP/1.1" or "HTTP/2".
std/net/http/client.fb, line 119
-
Its status, like 200 or 404.
-
Its headers.
-
Its body's bytes.
-
The URL it came from, after the redirects followed.
-
The HTTP it came over: "HTTP/1.1" or "HTTP/2".
-
Public New(status: Int, headers: Headers, bytes: UInt8[], url: String, version: String = "HTTP/1.1")constructor #A response of
status, withheadersandbytes, fromurl, overversion. -
The body as text, its UTF-8, with U+FFFD for each byte that doesn't start well-formed UTF-8.
-
Whether its Status says the request worked: from 200 to 299.
-
Throws a NetError of HTTP_STATUS unless IsSuccess.
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.
std/net/http/client.fb, line 172
-
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.
Http
struct
Public Struct Http
One request each, through a client of its own, with HttpClient's settings as they start.
std/net/http/client.fb, line 558
-
Sends a GET to
url, through a client of its own, and gives the response. -
The text of
url's response. Throws a NetError of HTTP_STATUS when its status isn't success. -
The bytes of
url's response. Throws a NetError of HTTP_STATUS when its status isn't success. -
Sends a HEAD to
url, through a client of its own, and gives the response. -
Public Static Method Post(url: String, body: Body): HttpResponse Throws NetError, TimeoutErrorstatic method #Sends a POST of
bodytourl, through a client of its own, and gives the response. -
Public Static Method Put(url: String, body: Body): HttpResponse Throws NetError, TimeoutErrorstatic method #Sends a PUT of
bodytourl, through a client of its own, and gives the response. -
Public Static Method Patch(url: String, body: Body): HttpResponse Throws NetError, TimeoutErrorstatic method #Sends a PATCH of
bodytourl, through a client of its own, and gives the response. -
Public Static Method Delete(url: String): HttpResponse Throws NetError, TimeoutErrorstatic method #Sends a DELETE to
url, through a client of its own, and gives the response. -
Public Static Method Download(url: String, progress: Func(Int64, Int64)? = Null): UInt8[] Throws NetError, TimeoutErrorstatic method #url's body, as HttpClient's Download gives it. -
Public Static Method Download(url: String, path: Path, progress: Func(Int64, Int64)? = Null) Throws NetError, TimeoutError, IOErrorstatic method #Writes
url's body to the file atpath, as HttpClient's Download does. -
Public Static Async Method GetAsync(url: String): HttpResponse Throws NetError, TimeoutErrorstatic method #What Get does, without blocking its thread: a call gives its Task.
-
Public Static Async Method GetTextAsync(url: String): String Throws NetError, TimeoutErrorstatic method #What GetText does, without blocking its thread: a call gives its Task.
-
Public Static Async Method GetBytesAsync(url: String): UInt8[] Throws NetError, TimeoutErrorstatic method #What GetBytes does, without blocking its thread: a call gives its Task.
-
Public Static Async Method PostAsync(url: String, body: Body): HttpResponse Throws NetError, TimeoutErrorstatic method #What Post does, without blocking its thread: a call gives its Task.
-
Public Static Async Method DownloadAsync(url: String, progress: Func(Int64, Int64)? = Null): UInt8[] Throws NetError, TimeoutErrorstatic method #What Download does, without blocking its thread: a call gives its Task.
-
Public Static Async Method DownloadAsync(url: String, path: Path, progress: Func(Int64, Int64)? = Null) Throws NetError, TimeoutError, IOErrorstatic method #What Download does, without blocking its thread: a call gives its Task.
Report an issue with this page or with FutureBlitz on GitHub.