[][src]Module http_io::client

Code for making HTTP requests.

Examples

Making simple requests

use http_io::error::Result;
use std::fs::File;
use std::io;

fn main() -> Result<()> {
    // Stream contents of url to stdout
    let mut body = http_io::client::get("http://www.google.com")?;
    io::copy(&mut body, &mut std::io::stdout())?;

    // Stream contents of file to remote server
    let file = File::open("src/client.rs")?;
    http_io::client::put("http://www.google.com", file)?;
    Ok(())
}

Using the HttpRequestBuilder for more control

use http_io::client::HttpRequestBuilder;
use http_io::error::Result;
use http_io::url::Url;
use std::io;
use std::net::TcpStream;

fn main() -> Result<()> {
    let url: Url = "http://www.google.com".parse()?;
    let s = TcpStream::connect((url.authority.as_ref(), url.port()?))?;
    let mut response = HttpRequestBuilder::get(url)?.send(s)?.finish()?;
    println!("{:#?}", response.headers);
    io::copy(&mut response.body, &mut io::stdout())?;
    Ok(())
}

Using HttpClient to keep connections open

use http_io::client::HttpClient;
use http_io::error::Result;
use http_io::url::Url;
use std::io;

fn main() -> Result<()> {
    let url: Url = "http://www.google.com".parse()?;
    let mut client = HttpClient::<std::net::TcpStream>::new();
    for path in &["/", "/favicon.ico", "/robots.txt"] {
        let mut url = url.clone();
        url.path = path.parse()?;
        io::copy(&mut client.get(url)?.finish()?.body, &mut io::stdout())?;
    }
    Ok(())
}

Structs

HttpClient

An HTTP client that keeps connections open.

HttpRequestBuilder

A struct for building up an HTTP request.

Traits

StreamConnector

Represents the ability to connect an abstract stream to some destination address.

Functions

get

Execute a GET request.

put

Execute a PUT request.