mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-21 19:13:07 +00:00
demonstrate basic http authentication (#605)
This commit is contained in:
parent
1b9a276ed2
commit
752b035c1a
4 changed files with 42 additions and 0 deletions
|
@ -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)
|
||||
|
|
|
@ -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}}
|
||||
|
|
5
src/web/clients/authentication.md
Normal file
5
src/web/clients/authentication.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
# Authentication
|
||||
|
||||
{{#include authentication/basic.md}}
|
||||
|
||||
{{#include ../../links.md}}
|
28
src/web/clients/authentication/basic.md
Normal file
28
src/web/clients/authentication/basic.md
Normal 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
|
Loading…
Reference in a new issue