pub struct HttpUrlBuilder { /* private fields */ }Expand description
Builder for constructing HTTP/HTTPS URLs programmatically.
HttpUrlBuilder is the focus of this crate. It accumulates URL components
as decoded Rust strings and, on build / build_url,
hands them to the url crate, which performs all percent-encoding,
normalization and validation according to the WHATWG URL Standard.
§Example
use http_url::{HttpUrl, Scheme};
let url = HttpUrl::builder()
.scheme(Scheme::Https)
.host("example.com")
.add_path_segment("api")
.add_path_segment("v1")
.add_query_parameter("key", "value")
.fragment("section")
.build()
.unwrap();
assert_eq!(url.to_string(), "https://example.com/api/v1?key=value#section");Implementations§
Source§impl HttpUrlBuilder
impl HttpUrlBuilder
Sourcepub fn from_url(url: Url) -> Result<Self>
pub fn from_url(url: Url) -> Result<Self>
Create a builder pre-populated from an existing url::Url.
The URL must have an http or https scheme. The returned builder is
ready to be modified and rebuilt via build or
build_url.
§Example
use http_url::HttpUrlBuilder;
let u = url::Url::parse("https://example.com/a/b?q=1").unwrap();
let url = HttpUrlBuilder::from_url(u)
.unwrap()
.add_path_segment("c")
.build()
.unwrap();
assert_eq!(url.as_url().path(), "/a/b/c");Sourcepub fn parse(url: &str) -> Result<Self>
pub fn parse(url: &str) -> Result<Self>
Create a builder pre-populated by parsing a URL string.
Equivalent to HttpUrlBuilder::from_url applied to the result of
url::Url::parse.
Sourcepub fn host(self, host: &str) -> Self
pub fn host(self, host: &str) -> Self
Set the host (domain name or IP address literal, including bracketed
IPv6 such as [::1]).
Sourcepub fn port(self, port: u16) -> Self
pub fn port(self, port: u16) -> Self
Set the port. Use remove_port to revert to the
scheme’s default.
Sourcepub fn remove_port(self) -> Self
pub fn remove_port(self) -> Self
Revert to the scheme’s default port.
Sourcepub fn add_path_segment(self, segment: &str) -> Self
pub fn add_path_segment(self, segment: &str) -> Self
Add a single path segment (decoded form; it is percent-encoded on build).
Sourcepub fn add_path_segments(self, segments: &[&str]) -> Self
pub fn add_path_segments(self, segments: &[&str]) -> Self
Add multiple path segments at once (decoded form).
Sourcepub fn set_path(self, path: &str) -> Self
pub fn set_path(self, path: &str) -> Self
Replace the entire path with the segments of path (decoded form).
path is split on /; a leading / is ignored and empty segments are
dropped. Each segment is stored as-is (decoded) and re-encoded on
build. No percent-decoding is applied to the input — pass a decoded
path.
Sourcepub fn add_query_parameter(self, name: &str, value: &str) -> Self
pub fn add_query_parameter(self, name: &str, value: &str) -> Self
Add a query parameter (name and value in decoded form). Both are
percent-encoded on build using application/x-www-form-urlencoded
semantics (space becomes +).
Sourcepub fn remove_query_parameter(self, name: &str) -> Self
pub fn remove_query_parameter(self, name: &str) -> Self
Remove all query parameters with the given name.
Sourcepub fn clear_query_parameters(self) -> Self
pub fn clear_query_parameters(self) -> Self
Clear all query parameters.
Sourcepub fn remove_fragment(self) -> Self
pub fn remove_fragment(self) -> Self
Remove the fragment.
Sourcepub fn build_url(self) -> Result<Url>
pub fn build_url(self) -> Result<Url>
Trait Implementations§
Source§impl Clone for HttpUrlBuilder
impl Clone for HttpUrlBuilder
Source§fn clone(&self) -> HttpUrlBuilder
fn clone(&self) -> HttpUrlBuilder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more