From 761054fd01c32e1c6dcefdd56aed156f18a78adb Mon Sep 17 00:00:00 2001 From: Garrett Squire Date: Wed, 17 May 2017 22:45:50 -0700 Subject: [PATCH] add an example for making a HEAD request with a timeout --- src/intro.md | 2 ++ src/net.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/intro.md b/src/intro.md index fff80e0..c6c691f 100644 --- a/src/intro.md +++ b/src/intro.md @@ -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 diff --git a/src/net.md b/src/net.md index d2d28c0..8704edb 100644 --- a/src/net.md +++ b/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 + +## 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 ## Create and delete Gist with GitHub API @@ -509,6 +559,7 @@ quick_main!(run); [`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