3 [libcurl] bindings for Rust
5 [![Latest Version](https://img.shields.io/crates/v/curl.svg)](https://crates.io/crates/curl)
6 [![Documentation](https://docs.rs/curl/badge.svg)](https://docs.rs/curl)
7 [![License](https://img.shields.io/github/license/alexcrichton/curl-rust.svg)](LICENSE)
8 [![Build](https://github.com/alexcrichton/curl-rust/workflows/CI/badge.svg)](https://github.com/alexcrichton/curl-rust/actions)
13 use std::io::{stdout, Write};
17 // Print a web page onto stdout
19 let mut easy = Easy::new();
20 easy.url("https://www.rust-lang.org/").unwrap();
21 easy.write_function(|data| {
22 stdout().write_all(data).unwrap();
25 easy.perform().unwrap();
27 println!("{}", easy.response_code().unwrap());
34 // Capture output into a local `Vec`.
36 let mut dst = Vec::new();
37 let mut easy = Easy::new();
38 easy.url("https://www.rust-lang.org/").unwrap();
40 let mut transfer = easy.transfer();
41 transfer.write_function(|data| {
42 dst.extend_from_slice(data);
45 transfer.perform().unwrap();
49 ## Post / Put requests
51 The `put` and `post` methods on `Easy` can configure the method of the HTTP
52 request, and then `read_function` can be used to specify how data is filled in.
53 This interface works particularly well with types that implement `Read`.
60 let mut data = "this is the body".as_bytes();
62 let mut easy = Easy::new();
63 easy.url("http://www.example.com/upload").unwrap();
64 easy.post(true).unwrap();
65 easy.post_field_size(data.len() as u64).unwrap();
67 let mut transfer = easy.transfer();
68 transfer.read_function(|buf| {
69 Ok(data.read(buf).unwrap_or(0))
71 transfer.perform().unwrap();
77 Custom headers can be specified as part of the request:
80 use curl::easy::{Easy, List};
83 let mut easy = Easy::new();
84 easy.url("http://www.example.com").unwrap();
86 let mut list = List::new();
87 list.append("Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==").unwrap();
88 easy.http_headers(list).unwrap();
89 easy.perform().unwrap();
95 The handle can be re-used across multiple requests. Curl will attempt to
96 keep the connections alive.
102 let mut handle = Easy::new();
104 handle.url("http://www.example.com/foo").unwrap();
105 handle.perform().unwrap();
107 handle.url("http://www.example.com/bar").unwrap();
108 handle.perform().unwrap();
114 The libcurl library provides support for sending multiple requests
115 simultaneously through the "multi" interface. This is currently bound in the
116 `multi` module of this crate and provides the ability to execute multiple
117 transfers simultaneously. For more information, see that module.
121 By default, this crate will attempt to dynamically link to the system-wide
122 libcurl and the system-wide SSL library. Some of this behavior can be customized
123 with various Cargo features:
125 - `ssl`: Enable SSL/TLS support using the platform-default TLS backend. On Windows this is [Schannel], on macOS [Secure Transport], and [OpenSSL] (or equivalent) on all other platforms. Enabled by default.
126 - `mesalink`: Enable SSL/TLS support via [MesaLink], an alternative TLS backend written in Rust based on [Rustls]. MesaLink is always statically linked. Disabled by default.
127 - `http2`: Enable HTTP/2 support via libnghttp2. Disabled by default.
128 - `static-curl`: Use a bundled libcurl version and statically link to it. Disabled by default.
129 - `static-ssl`: Use a bundled OpenSSL version and statically link to it. Only applies on platforms that use OpenSSL. Disabled by default.
130 - `spnego`: Enable SPNEGO support. Disabled by default.
131 - `upkeep_7_62_0`: Enable curl_easy_upkeep() support, introduced in curl 7.62.0. Disabled by default.
135 The bindings have been developed using curl version 7.24.0. They should
136 work with any newer version of curl and possibly with older versions,
137 but this has not been tested.
141 ### Curl built against the NSS SSL library
143 If you encounter the following error message:
146 [77] Problem with the SSL CA cert (path? access rights?)
149 That means most likely, that curl was linked against `libcurl-nss.so` due to
150 installed libcurl NSS development files, and that the required library
151 `libnsspem.so` is missing. See also the curl man page: "If curl is built
152 against the NSS SSL library, the NSS PEM PKCS#11 module (`libnsspem.so`) needs to be available for this option to work properly."
154 In order to avoid this failure you can either
156 * install the missing library (e.g. Debian: `nss-plugin-pem`), or
157 * remove the libcurl NSS development files (e.g. Debian: `libcurl4-nss-dev`) and
162 The `curl-rust` crate is licensed under the MIT license, see [`LICENSE`](LICENSE) for more
166 [libcurl]: https://curl.haxx.se/libcurl/
167 [MesaLink]: https://mesalink.io/
168 [OpenSSL]: https://www.openssl.org/
169 [Rustls]: https://github.com/ctz/rustls
170 [Schannel]: https://docs.microsoft.com/en-us/windows/win32/com/schannel
171 [Secure Transport]: https://developer.apple.com/documentation/security/secure_transport