HttpClient
struct HttpClient
A HTTP client.
Example
nv,no_run
use std.net.http.client.HttpClient;
let client = HttpClient.new();
fn main() throws {
let resp = try client.get("https://example.com");
println(resp.status());
try println(resp.text());
}
Methods
new
pub fn new(connector: HttpConnector = DefaultHttpConnector.new(), default_headers: Headers | <string, string>? = nil, user_agent: string? = nil, enable_compression: bool = true, enable_redirect: bool = true, max_redirect_count: int? = 10): HttpClient
[src]
pub fn new(
connector: HttpConnector = DefaultHttpConnector.new(),
default_headers: (Headers | <string, string>)? = nil,
user_agent: string? = nil,
enable_compression: bool = true,
enable_redirect: bool = true,
max_redirect_count: int? = 10
): HttpClient {
let requester: Requester = RawRequester.new(connector);
if (let default_headers = default_headers) {
requester = DefaultHeaders.new(requester, default_headers);
}
if (let user_agent = user_agent) {
requester = UserAgent.new(requester, user_agent);
}
if (enable_redirect) {
requester = Redirect.new(requester, max_redirect_count);
}
if (enable_compression) {
requester = Compression.new(requester);
}
return HttpClient { requester };
}
Creates a new HTTP client.
new
pub fn new(connector: HttpConnector = DefaultHttpConnector.new(), default_headers: Headers | <string, string>? = nil, user_agent: string? = nil, enable_compression: bool = true, enable_redirect: bool = true, max_redirect_count: int? = 10): HttpClient
[src]
pub fn new(
connector: HttpConnector = DefaultHttpConnector.new(),
default_headers: (Headers | <string, string>)? = nil,
user_agent: string? = nil,
enable_compression: bool = true,
enable_redirect: bool = true,
max_redirect_count: int? = 10
): HttpClient {
let requester: Requester = RawRequester.new(connector);
if (let default_headers = default_headers) {
requester = DefaultHeaders.new(requester, default_headers);
}
if (let user_agent = user_agent) {
requester = UserAgent.new(requester, user_agent);
}
if (enable_redirect) {
requester = Redirect.new(requester, max_redirect_count);
}
if (enable_compression) {
requester = Compression.new(requester);
}
return HttpClient { requester };
}
Creates a new HTTP client.
Creates a new HTTP client.
Arguments
connector
: The HTTP connector to use. Default isDefaultHttpConnector
.default_headers
: Default headers to include in every request.user_agent
: TheUser-Agent
header to include in every request.enable_compression
: Whether to enable compression. Default istrue
.enable_redirect
: Whether to enable redirect. Default istrue
.max_redirect_count
: The maximum number of redirects to follow. Default is10
,nil
means no limit.
Example
nv
use std.net.http.client.HttpClient;
use std.net.http.Headers;
let client = HttpClient.new(
default_headers: Headers.from_map({
"user-agent": "navi",
}),
);
[src]
pub fn request(self, request: Request): Response throws {
return try self.requester.request(request);
}
Sends a HTTP request.
[src]
pub fn request(self, request: Request): Response throws {
return try self.requester.request(request);
}
Sends a HTTP request.
Sends a HTTP request.
Example
nv,no_run
use std.net.http.client.{HttpClient, Request};
fn main() throws {
let client = HttpClient.new();
// Send a GET request
let resp = try client.request(Request.get("https://example.com"));
// Print the response status and body
println(resp.status());
try println(resp.text());
}
Example
nv,no_run
use std.net.http.client.{HttpClient, Request};
fn main() throws {
let client = HttpClient.new();
// Send a POST request with a custom user-agent header
let req = try Request.post("https://example.com")
.set_header("user-agent", "navi")
.set_text("Hello, world!");
let resp = try client.request(req);
// Print the response status and body
println(resp.status());
try println(resp.text());
}
[src]
pub fn get(self, uri: Uri | string): Response throws {
return try self.request(Request.new(Method.Get, uri));
}
Sends a GET request.
[src]
pub fn get(self, uri: Uri | string): Response throws {
return try self.request(Request.new(Method.Get, uri));
}
Sends a GET request.
Sends a GET request.
Example
nv,no_run
use std.net.http.client.HttpClient;
let client = HttpClient.new();
fn main() throws {
let resp = try client.get("https://example.com");
println(resp.status());
try println(resp.text());
}