Skip to content

Commit

Permalink
Merge pull request #145 from jaemk/requestbuilder_docs
Browse files Browse the repository at this point in the history
RequestBuilder doc improvements
  • Loading branch information
seanmonstar authored Jun 9, 2017
2 parents 5c9df5f + 3cbb6cd commit eb38461
Showing 1 changed file with 69 additions and 4 deletions.
73 changes: 69 additions & 4 deletions src/request.rs
Original file line number Diff line number Diff line change
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

0 comments on commit eb38461

Please sign in to comment.