Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestBuilder doc improvements #145

Merged
merged 1 commit into from Jun 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 69 additions & 4 deletions src/request.rs
Expand Up @@ -87,12 +87,10 @@ impl RequestBuilder {
/// Add a `Header` to this Request.
///
/// ```rust
/// # use reqwest::Error;
/// #
/// # fn run() -> Result<(), Error> {
/// use reqwest::header::UserAgent;
/// let client = reqwest::Client::new()?;
///
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// let client = reqwest::Client::new()?;
/// let res = client.get("https://www.rust-lang.org")?
/// .header(UserAgent("foo".to_string()))
/// .send()?;
Expand All @@ -109,12 +107,44 @@ impl RequestBuilder {
/// Add a set of Headers to the existing ones on this Request.
///
/// The headers will be merged in to any already set.
///
/// ```rust
/// use reqwest::header::{Headers, UserAgent, ContentType};
/// # use std::fs;
///
/// fn construct_headers() -> Headers {
/// let mut headers = Headers::new();
/// headers.set(UserAgent("reqwest".to_string()));
/// headers.set(ContentType::png());
/// headers
/// }
///
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// let file = fs::File::open("much_beauty.png")?;
/// let client = reqwest::Client::new()?;
/// let res = client.post("http://httpbin.org/post")?
/// .headers(construct_headers())
/// .body(file)
/// .send()?;
/// # Ok(())
/// # }
/// ```
pub fn headers(&mut self, headers: ::header::Headers) -> &mut RequestBuilder {
self.request_mut().headers.extend(headers.iter());
self
}

/// Enable HTTP basic authentication.
///
/// ```rust
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// let client = reqwest::Client::new()?;
/// let resp = client.delete("http://httpbin.org/delete")?
/// .basic_auth("admin", Some("good password"))
/// .send()?;
/// # Ok(())
/// # }
/// ```
pub fn basic_auth<U, P>(&mut self, username: U, password: Option<P>) -> &mut RequestBuilder
where
U: Into<String>,
Expand All @@ -127,6 +157,41 @@ impl RequestBuilder {
}

/// Set the request body.
///
/// ```rust
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// let client = reqwest::Client::new()?;
/// let res = client.post("http://httpbin.org/post")?
/// .body("from a &str!")
/// .send()?;
/// # Ok(())
/// # }
/// ```
///
/// ```rust
/// # use std::fs;
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// let file = fs::File::open("from_a_file.txt")?;
/// let client = reqwest::Client::new()?;
/// let res = client.post("http://httpbin.org/post")?
/// .body(file)
/// .send()?;
/// # Ok(())
/// # }
/// ```
///
/// ```rust
/// # use std::fs;
/// # fn run() -> Result<(), Box<::std::error::Error>> {
/// // from bytes!
/// let bytes: Vec<u8> = vec![1, 10, 100];
/// let client = reqwest::Client::new()?;
/// let res = client.post("http://httpbin.org/post")?
/// .body(bytes)
/// .send()?;
/// # Ok(())
/// # }
/// ```
pub fn body<T: Into<Body>>(&mut self, body: T) -> &mut RequestBuilder {
self.request_mut().body = Some(body.into());
self
Expand Down