demonstrate basic http authentication (#605)

This commit is contained in:
Stefan Mesken 2021-01-07 17:17:49 +01:00 committed by GitHub
parent 1b9a276ed2
commit 752b035c1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 0 deletions

View file

@ -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)

View file

@ -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}}

View file

@ -0,0 +1,5 @@
# Authentication
{{#include authentication/basic.md}}
{{#include ../../links.md}}

View file

@ -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<String> = 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