Add ureq::request_url and Agent::request_url. (#226)

These let a user pass an already-parsed Url.
This commit is contained in:
Jacob Hoffman-Andrews
2020-11-21 22:11:15 -08:00
committed by GitHub
parent 0321ea043d
commit e92bf0b4bb
3 changed files with 102 additions and 12 deletions

View File

@@ -207,6 +207,7 @@ mod cookies;
#[cfg(feature = "json")]
pub use serde_json::json;
use url::Url;
#[cfg(test)]
mod test;
@@ -265,18 +266,46 @@ pub fn agent() -> Agent {
return testserver::test_agent();
}
/// Make a request setting the HTTP method via a string.
/// Make a request with the HTTP verb as a parameter.
///
/// This allows making requests with verbs that don't have a dedicated
/// method.
///
/// If you've got an already-parsed [Url], try [request_url][request_url].
///
/// ```
/// # fn main() -> Result<(), ureq::Error> {
/// # ureq::is_test(true);
/// ureq::request("GET", "http://example.com").call()?;
/// let resp: ureq::Response = ureq::request("OPTIONS", "http://example.com/")
/// .call()?;
/// # Ok(())
/// # }
/// ```
pub fn request(method: &str, path: &str) -> Request {
agent().request(method, path)
}
/// Make a request using an already-parsed [Url].
///
/// This is useful if you've got a parsed Url from some other source, or if
/// you want to parse the URL and then modify it before making the request.
/// If you'd just like to pass a String or a `&str`, try [request][request()].
///
/// ```
/// # fn main() -> Result<(), ureq::Error> {
/// # ureq::is_test(true);
/// use url::Url;
/// let agent = ureq::agent();
///
/// let mut url: Url = "http://example.com/some-page".parse().unwrap();
/// url.set_path("/robots.txt");
/// let resp: ureq::Response = ureq::request_url("GET", &url)
/// .call()?;
/// # Ok(())
/// # }
/// ```
pub fn request_url(method: &str, url: &Url) -> Request {
agent().request_url(method, url)
}
/// Make a GET request.
pub fn get(path: &str) -> Request {