#curl #http

curl

Rust bindings to libcurl for making HTTP requests

66 releases

0.5.0 Jan 3, 2018
0.4.14 Jul 31, 2018
0.4.12 Apr 14, 2018
0.4.11 Jan 3, 2018
0.1.4 Dec 29, 2014

#5 in HTTP client

Download history 11580/week @ 2018-09-01 12521/week @ 2018-09-08 18990/week @ 2018-09-15 16232/week @ 2018-09-22 15854/week @ 2018-09-29 19125/week @ 2018-10-06 17019/week @ 2018-10-13 15876/week @ 2018-10-20 14581/week @ 2018-10-27 15084/week @ 2018-11-03 15730/week @ 2018-11-10 15100/week @ 2018-11-17 12811/week @ 2018-11-24

50,701 downloads per month
Used in 161 crates (91 directly)

MIT license

7.5MB
156K SLoC

C 116K SLoC // 0.2% comments Python 15K SLoC // 0.1% comments Perl 14K SLoC // 0.2% comments Rust 4.5K SLoC // 0.4% comments D 3K SLoC Batch 1.5K SLoC Shell 1K SLoC // 0.3% comments C++ 488 SLoC // 0.2% comments VBScript 338 SLoC // 0.1% comments

curl-rust

libcurl bindings for Rust

Build Status Build status

Documentation

Quick Start

extern crate curl;

use std::io::{stdout, Write};

use curl::easy::Easy;

// Print a web page onto stdout
fn main() {
    let mut easy = Easy::new();
    easy.url("https://www.rust-lang.org/").unwrap();
    easy.write_function(|data| {
        stdout().write_all(data).unwrap();
        Ok(data.len())
    }).unwrap();
    easy.perform().unwrap();

    println!("{}", easy.response_code().unwrap());
}
extern crate curl;

use curl::easy::Easy;

// Capture output into a local `Vec`.
fn main() {
    let mut dst = Vec::new();
    let mut easy = Easy::new();
    easy.url("https://www.rust-lang.org/").unwrap();

    let mut transfer = easy.transfer();
    transfer.write_function(|data| {
        dst.extend_from_slice(data);
        Ok(data.len())
    }).unwrap();
    transfer.perform().unwrap();
}

Post / Put requests

The put and post methods on Easy can configure the method of the HTTP request, and then read_function can be used to specify how data is filled in. This interface works particularly well with types that implement Read.

extern crate curl;

use std::io::Read;
use curl::easy::Easy;

fn main() {
    let mut data = "this is the body".as_bytes();

    let mut easy = Easy::new();
    easy.url("http://www.example.com/upload").unwrap();
    easy.post(true).unwrap();
    easy.post_field_size(data.len() as u64).unwrap();

    let mut transfer = easy.transfer();
    transfer.read_function(|buf| {
        Ok(data.read(buf).unwrap_or(0))
    }).unwrap();
    transfer.perform().unwrap();
}

Custom headers

Custom headers can be specified as part of the request:

extern crate curl;

use curl::easy::{Easy, List};

fn main() {
    let mut easy = Easy::new();
    easy.url("http://www.example.com").unwrap();

    let mut list = List::new();
    list.append("Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==").unwrap();
    easy.http_headers(list).unwrap();
    easy.perform().unwrap();
}

Keep alive

The handle can be re-used across multiple requests. Curl will attempt to keep the connections alive.

extern crate curl;

use curl::easy::Easy;

fn main() {
    let mut handle = Easy::new();

    handle.url("http://www.example.com/foo").unwrap();
    handle.perform().unwrap();

    handle.url("http://www.example.com/bar").unwrap();
    handle.perform().unwrap();
}

Multiple requests

The libcurl library provides support for sending multiple requests simultaneously through the "multi" interface. This is currently bound in the multi module of this crate and provides the ability to execute multiple transfers simultaneously. For more information, see that module.

Building

By default, this crate will attempt to dynamically link to the system-wide libcurl and the system-wide SSL library. Some of this behavior can be customized with various Cargo features:

  • ssl: Enable SSL support. Enabled by default.
  • http2: Enable HTTP/2 support via libnghttp2. Disabled by default.
  • static-curl: Use a bundled libcurl version and statically link to it. Disabled by default.
  • static-ssl: Use a bundled OpenSSL version and statically link to it. Only applies on platforms that use OpenSSL. Disabled by default.
  • spengo: Enable SPENGO support. Disabled by default.

Version Support

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.

License

The curl-rust crate is licensed under the MIT license, see LICENSE for more details.

Dependencies

~3.5MB
~67K SLoC