From 752b035c1ae1646c999b4d7c39c78d67215d5893 Mon Sep 17 00:00:00 2001 From: Stefan Mesken Date: Thu, 7 Jan 2021 17:17:49 +0100 Subject: [PATCH] demonstrate basic http authentication (#605) --- src/SUMMARY.md | 1 + src/web.md | 8 +++++++ src/web/clients/authentication.md | 5 +++++ src/web/clients/authentication/basic.md | 28 +++++++++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 src/web/clients/authentication.md create mode 100644 src/web/clients/authentication/basic.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 85a62cb..9318d35 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -65,3 +65,4 @@ - [Making Requests](web/clients/requests.md) - [Calling a Web API](web/clients/apis.md) - [Downloads](web/clients/download.md) + - [Web Authentication](web/clients/authentication.md) diff --git a/src/web.md b/src/web.md index 3bf9314..461de9e 100644 --- a/src/web.md +++ b/src/web.md @@ -39,6 +39,12 @@ | [Make a partial download with HTTP range headers][ex-progress-with-range] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] | | [POST a file to paste-rs][ex-file-post] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] | +## Web Authentication + +| Recipe | Crates | Categories | +|--------|--------|------------| +| [Basic Authentication][ex-basic-authentication] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] | + [ex-extract-links-webpage]: web/scraping.html#extract-all-links-from-a-webpage-html [ex-check-broken-links]: web/scraping.html#check-a-webpage-for-broken-links [ex-extract-mediawiki-links]: web/scraping.html#extract-all-unique-links-from-a-mediawiki-markup @@ -64,4 +70,6 @@ [ex-progress-with-range]: web/clients/download.html#make-a-partial-download-with-http-range-headers [ex-file-post]: web/clients/download.html#post-a-file-to-paste-rs +[ex-basic-authentication]: web/clients/authentication.html#basic-authentication + {{#include links.md}} diff --git a/src/web/clients/authentication.md b/src/web/clients/authentication.md new file mode 100644 index 0000000..91700fd --- /dev/null +++ b/src/web/clients/authentication.md @@ -0,0 +1,5 @@ +# Authentication + +{{#include authentication/basic.md}} + +{{#include ../../links.md}} diff --git a/src/web/clients/authentication/basic.md b/src/web/clients/authentication/basic.md new file mode 100644 index 0000000..650cbb9 --- /dev/null +++ b/src/web/clients/authentication/basic.md @@ -0,0 +1,28 @@ +## Basic Authentication + +[![reqwest-badge]][reqwest] [![cat-net-badge]][cat-net] + +Uses [`reqwest::RequestBuilder::basic_auth`] to perform a basic HTTP authentication. + +```rust,edition2018,no_run +use reqwest::blocking::Client; +use reqwest::Error; + +fn main() -> Result<(), Error> { + let client = Client::new(); + + let user_name = "testuser".to_string(); + let password: Option = None; + + let response = client + .get("https://httpbin.org/") + .basic_auth(user_name, password) + .send(); + + println!("{:?}", response); + + Ok(()) +} +``` + +[`reqwest::RequestBuilder::basic_auth`]: https://docs.rs/reqwest/*/reqwest/struct.RequestBuilder.html#method.basic_auth