libcurl bindings for Rust. This project is in active development and could (will probably) break API compatibility at any time.
extern crate curl;
use curl::http;
pub fn main() {
let resp = http::handle()
.get("http://www.example.com")
.exec().unwrap();
println!("code={}; headers={:?}; body={:?}",
resp.get_code(), resp.get_headers(), resp.get_body());
}
Response header names are automatically lower cased.
Both of these methods expect that a request body is provided. A request
body can be a &[u8]
, &str
, or &Reader
. For example:
let resp = http::handle()
.post("http://www.example.com/upload", "this is the body")
.exec().unwrap();
Custom headers can be specified as part of the request:
http::handle()
.get("http://www.example.com")
.header("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==")
.exec();
The handle can be re-used across multiple requests. Curl will attempt to keep the connections alive.
let handle = http::handle();
let resp1 = handle.get("http://www.example.com/foo").exec().unwrap();
let resp2 = handle.get("http://www.example.com/bar").exec().unwrap();
The bindings have been developed using curl version 7.24.0. They should work with any newer version of curl and possibly with older versions, but this has not been tested.