mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-26 05:20:23 +00:00
add an example for making a HEAD request with a timeout
This commit is contained in:
parent
4a5c675e66
commit
761054fd01
2 changed files with 53 additions and 0 deletions
|
@ -53,6 +53,7 @@ community. It needs and welcomes help. For details see
|
|||
| [Make a HTTP GET request after parsing a URL][ex-url-basic] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] |
|
||||
| [Download a file to a temporary directory][ex-url-download] | [![reqwest-badge]][reqwest] [![tempdir-badge]][tempdir] | [![cat-net-badge]][cat-net] [![cat-filesystem-badge]][cat-filesystem] |
|
||||
| [Query the GitHub API][ex-rest-get] | [![reqwest-badge]][reqwest] [![serde-badge]][serde] | [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding] |
|
||||
| [Check if an API Resource Exists][ex-rest-head] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] |
|
||||
| [Create and delete Gist with GitHub API][ex-rest-post] | [![reqwest-badge]][reqwest] [![serde-badge]][serde] | [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding] |
|
||||
|
||||
## [Application development](app.html)
|
||||
|
@ -144,6 +145,7 @@ Keep lines sorted.
|
|||
[ex-rand-float]: basics.html#ex-rand-float
|
||||
[ex-rand-range]: basics.html#ex-rand-range
|
||||
[ex-rayon-iter-mut]: concurrency.html#ex-rayon-iter-mut
|
||||
[ex-rest-head]: net.html#ex-rest-head
|
||||
[ex-rest-get]: net.html#ex-rest-get
|
||||
[ex-rest-post]: net.html#ex-rest-post
|
||||
[ex-std-read-lines]: basics.html#ex-std-read-lines
|
||||
|
|
51
src/net.md
51
src/net.md
|
@ -10,6 +10,7 @@
|
|||
| [Make a HTTP GET request][ex-url-basic] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] |
|
||||
| [Download a file to a temporary directory][ex-url-download] | [![reqwest-badge]][reqwest] [![tempdir-badge]][tempdir] | [![cat-net-badge]][cat-net] [![cat-filesystem-badge]][cat-filesystem] |
|
||||
| [Query the GitHub API][ex-rest-get] | [![reqwest-badge]][reqwest] [![serde-badge]][serde] | [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding] |
|
||||
| [Check if an API Resource Exists][ex-rest-head] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] |
|
||||
| [Create and delete Gist with GitHub API][ex-rest-post] | [![reqwest-badge]][reqwest] [![serde-badge]][serde] | [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding] |
|
||||
|
||||
[ex-url-parse]: #ex-url-parse
|
||||
|
@ -406,6 +407,55 @@ fn run() -> Result<()> {
|
|||
quick_main!(run);
|
||||
```
|
||||
|
||||
[ex-rest-head]: #ex-rest-head
|
||||
<a name="ex-rest-head"/>
|
||||
## Check if a resource exists using a HEAD request
|
||||
|
||||
[![reqwest-badge]][reqwest] [![cat-net-badge]][cat-net]
|
||||
|
||||
Query the [GitHub Users Endpoint](https://api.github.com/users) using a HEAD request and then inspect the
|
||||
response code to determine success. The [`reqwest::Client`] offers a timeout option which accepts a [`Duration`] as an argument. This is a quick way to query a rest resource without
|
||||
needing to receive a body.
|
||||
|
||||
```rust,no_run
|
||||
#[macro_use]
|
||||
extern crate error_chain;
|
||||
|
||||
extern crate reqwest;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use reqwest::Client;
|
||||
|
||||
error_chain! {
|
||||
foreign_links {
|
||||
Reqwest(reqwest::Error);
|
||||
}
|
||||
}
|
||||
|
||||
fn run() -> Result<()> {
|
||||
let request_url = "https://api.github.com/users/ferris-the-crab";
|
||||
println!("{}", request_url);
|
||||
|
||||
// Make a timeout for our request.
|
||||
let timeout = Duration::new(5, 0);
|
||||
|
||||
let mut client = Client::new()?;
|
||||
client.timeout(timeout);
|
||||
let response = client.head(request_url).send()?;
|
||||
|
||||
if response.status().is_success() {
|
||||
println!("ferris-the-crab is a user!");
|
||||
} else {
|
||||
println!("ferris-the-crab is not a user!");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
quick_main!(run);
|
||||
```
|
||||
|
||||
[ex-rest-post]: #ex-rest-post
|
||||
<a name="ex-rest-post"/>
|
||||
## Create and delete Gist with GitHub API
|
||||
|
@ -509,6 +559,7 @@ quick_main!(run);
|
|||
<!-- Reference -->
|
||||
|
||||
[`io::copy`]: https://doc.rust-lang.org/std/io/fn.copy.html
|
||||
[`Duration`]: https://doc.rust-lang.org/nightly/std/time/struct.Duration.html
|
||||
[`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
|
||||
[`Url`]: https://docs.rs/url/1.*/url/struct.Url.html
|
||||
[`parse`]: https://docs.rs/url/1.*/url/struct.Url.html#method.parse
|
||||
|
|
Loading…
Reference in a new issue