Skip to content

Commit 752b035

Browse files
demonstrate basic http authentication (#605)
1 parent 1b9a276 commit 752b035

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

Diff for: src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@
6565
- [Making Requests](web/clients/requests.md)
6666
- [Calling a Web API](web/clients/apis.md)
6767
- [Downloads](web/clients/download.md)
68+
- [Web Authentication](web/clients/authentication.md)

Diff for: src/web.md

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
| [Make a partial download with HTTP range headers][ex-progress-with-range] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] |
4040
| [POST a file to paste-rs][ex-file-post] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] |
4141

42+
## Web Authentication
43+
44+
| Recipe | Crates | Categories |
45+
|--------|--------|------------|
46+
| [Basic Authentication][ex-basic-authentication] | [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] |
47+
4248
[ex-extract-links-webpage]: web/scraping.html#extract-all-links-from-a-webpage-html
4349
[ex-check-broken-links]: web/scraping.html#check-a-webpage-for-broken-links
4450
[ex-extract-mediawiki-links]: web/scraping.html#extract-all-unique-links-from-a-mediawiki-markup
@@ -64,4 +70,6 @@
6470
[ex-progress-with-range]: web/clients/download.html#make-a-partial-download-with-http-range-headers
6571
[ex-file-post]: web/clients/download.html#post-a-file-to-paste-rs
6672

73+
[ex-basic-authentication]: web/clients/authentication.html#basic-authentication
74+
6775
{{#include links.md}}

Diff for: src/web/clients/authentication.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Authentication
2+
3+
{{#include authentication/basic.md}}
4+
5+
{{#include ../../links.md}}

Diff for: src/web/clients/authentication/basic.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Basic Authentication
2+
3+
[![reqwest-badge]][reqwest] [![cat-net-badge]][cat-net]
4+
5+
Uses [`reqwest::RequestBuilder::basic_auth`] to perform a basic HTTP authentication.
6+
7+
```rust,edition2018,no_run
8+
use reqwest::blocking::Client;
9+
use reqwest::Error;
10+
11+
fn main() -> Result<(), Error> {
12+
let client = Client::new();
13+
14+
let user_name = "testuser".to_string();
15+
let password: Option<String> = None;
16+
17+
let response = client
18+
.get("https://httpbin.org/")
19+
.basic_auth(user_name, password)
20+
.send();
21+
22+
println!("{:?}", response);
23+
24+
Ok(())
25+
}
26+
```
27+
28+
[`reqwest::RequestBuilder::basic_auth`]: https://docs.rs/reqwest/*/reqwest/struct.RequestBuilder.html#method.basic_auth

0 commit comments

Comments
 (0)