mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
awc: Add support for setting query from Serialize type for client request (#1130)
Signed-off-by: Jonathas-Conceicao <jadoliveira@inf.ufpel.edu.br>
This commit is contained in:
parent
ace98e3a1e
commit
f0612f7570
@ -1,5 +1,8 @@
|
||||
# Changes
|
||||
|
||||
## [0.2.8] - 2019-10-24
|
||||
|
||||
* Add support for setting query from Serialize type for client request.
|
||||
|
||||
## [0.2.7] - 2019-09-25
|
||||
|
||||
|
@ -382,6 +382,27 @@ impl ClientRequest {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the query part of the request
|
||||
pub fn query<T: Serialize>(
|
||||
mut self,
|
||||
query: &T,
|
||||
) -> Result<Self, serde_urlencoded::ser::Error> {
|
||||
let mut parts = self.head.uri.clone().into_parts();
|
||||
|
||||
if let Some(path_and_query) = parts.path_and_query {
|
||||
let query = serde_urlencoded::to_string(query)?;
|
||||
let path = path_and_query.path();
|
||||
parts.path_and_query = format!("{}?{}", path, query).parse().ok();
|
||||
|
||||
match Uri::from_parts(parts) {
|
||||
Ok(uri) => self.head.uri = uri,
|
||||
Err(e) => self.err = Some(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Freeze request builder and construct `FrozenClientRequest`,
|
||||
/// which could be used for sending same request multiple times.
|
||||
pub fn freeze(self) -> Result<FrozenClientRequest, FreezeRequestError> {
|
||||
@ -690,4 +711,13 @@ mod tests {
|
||||
"Bearer someS3cr3tAutht0k3n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn client_query() {
|
||||
let req = Client::new()
|
||||
.get("/")
|
||||
.query(&[("key1", "val1"), ("key2", "val2")])
|
||||
.unwrap();
|
||||
assert_eq!(req.get_uri().query().unwrap(), "key1=val1&key2=val2");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user