pub struct QueryBuilder<'a> { /* private fields */ }export-azure only.Expand description
A builder for setting query parameters on a URL.
This builder allows you to set multiple query parameters, overwriting any existing
values with the same key. Call build() to apply the changes.
Implementations§
Source§impl<'a> QueryBuilder<'a>
impl<'a> QueryBuilder<'a>
Sourcepub fn append_key_only(
&mut self,
key: impl Into<Cow<'a, str>>,
) -> &mut QueryBuilder<'a>
pub fn append_key_only( &mut self, key: impl Into<Cow<'a, str>>, ) -> &mut QueryBuilder<'a>
Appends a key without a value to the URL query string.
This is useful for boolean flags or markers in query strings that don’t require a value.
Returns &mut Self to allow chaining multiple calls.
§Examples
use typespec_client_core::http::{Url, UrlExt as _};
let mut url: Url = "https://contoso.com".parse().unwrap();
let mut query_builder = url.query_builder();
query_builder
.append_key_only("debug")
.append_pair("a", "1");
query_builder.build();
let params: Vec<_> = url.query_pairs().collect();
assert!(params.contains(&("debug".into(), "".into())));
assert!(params.contains(&("a".into(), "1".into())));Sourcepub fn append_pair(
&mut self,
key: impl Into<Cow<'a, str>>,
value: impl Into<Cow<'a, str>>,
) -> &mut QueryBuilder<'a>
pub fn append_pair( &mut self, key: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>, ) -> &mut QueryBuilder<'a>
Appends a query parameter to the URL.
If the key already exists, this adds an additional value for that key (allowing duplicates).
Use set_pair to overwrite existing values instead.
Both key and value accept types that can be converted to Cow<'a, str>, which includes
&'a str, String, and Cow<'a, str>. This avoids unnecessary allocations when using
string literals.
Returns &mut Self to allow chaining multiple calls.
§Examples
use typespec_client_core::http::{Url, UrlExt as _};
let mut url: Url = "https://contoso.com?a=1".parse().unwrap();
let mut query_builder = url.query_builder();
query_builder
.append_pair("a", "2")
.append_pair("b", "3");
query_builder.build();
let params: Vec<_> = url.query_pairs().collect();
assert!(params.contains(&("a".into(), "1".into())));
assert!(params.contains(&("a".into(), "2".into())));
assert!(params.contains(&("b".into(), "3".into())));Sourcepub fn set_pair(
&mut self,
key: impl Into<Cow<'a, str>>,
value: impl Into<Cow<'a, str>>,
) -> &mut QueryBuilder<'a>
pub fn set_pair( &mut self, key: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>, ) -> &mut QueryBuilder<'a>
Sets a query parameter, overwriting any existing value with the same key.
If the key already exists, all existing values for that key are replaced with the new value.
If the key doesn’t exist, it’s added to the end of the query parameters.
Use append_pair to add additional values without replacing existing ones.
Both key and value accept types that can be converted to Cow<'a, str>, which includes
&'a str, String, and Cow<'a, str>. This avoids unnecessary allocations when using
string literals.
Returns &mut Self to allow chaining multiple calls.
§Examples
use typespec_client_core::http::{Url, UrlExt as _};
let mut url: Url = "https://contoso.com?a=1&b=2".parse().unwrap();
let mut query_builder = url.query_builder();
query_builder
.set_pair("a", "new_value")
.set_pair("c", "3");
query_builder.build();
let params: Vec<_> = url.query_pairs().collect();
assert!(params.contains(&("a".into(), "new_value".into())));
assert!(params.contains(&("b".into(), "2".into())));
assert!(params.contains(&("c".into(), "3".into())));