Code Monkey home page Code Monkey logo

curl-rust's Introduction

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.

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.

curl-rust's People

Contributors

akosthekiss avatar alexcrichton avatar ben0x539 avatar carllerche avatar gifnksm avatar globin avatar hjr3 avatar ishitatsuyuki avatar jhgg avatar kaknife avatar klausi avatar krdln avatar kstep avatar kyledewey avatar lluchs avatar maxdymond avatar mcgoo avatar mneumann avatar mokosha avatar nbaksalyar avatar overminder avatar raindev avatar samuelleeuwenburg avatar silvio avatar simias avatar simonsapin avatar simonvandel avatar tomaka avatar tshepang avatar waldemarnt avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

jhowarth

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.