Expand description
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://abort.cc")?;
io::copy(&mut body, &mut std::io::stdout())?;
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
An HTTP client that keeps connections open.
A struct for building up an HTTP request.
Enums
Traits
Represents the ability to connect an abstract stream to some destination address.