Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 42 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "http_req"
version = "0.9.3"
version = "0.10.0"
license = "MIT"
description = "simple and lightweight HTTP client with built-in HTTPS support"
repository = "https://github.com/jayjamesjay/http_req"
Expand All @@ -11,24 +11,28 @@ keywords = ["http", "client", "request"]
edition = "2021"

[dependencies]
unicase = "^2.6"
unicase = "^2.7"

[features]
default = ["native-tls"]
rust-tls = ["rustls", "webpki", "webpki-roots"]
rust-tls = ["rustls", "webpki", "webpki-roots", "rustls-pemfile"]

[dependencies.native-tls]
version = "^0.2"
optional = true

[dependencies.rustls]
version = "^0.19"
version = "^0.21"
optional = true

[dependencies.rustls-pemfile]
version = "^1.0"
optional = true

[dependencies.webpki]
version = "^0.21"
version = "^0.22"
optional = true

[dependencies.webpki-roots]
version = "^0.21"
version = "^0.25"
optional = true
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
# http_req
[![Rust](https://github.com/jayjamesjay/http_req/actions/workflows/rust.yml/badge.svg)](https://github.com/jayjamesjay/http_req/actions/workflows/rust.yml)
[![Crates.io](https://img.shields.io/badge/crates.io-v0.9.3-orange.svg?longCache=true)](https://crates.io/crates/http_req)
[![Docs.rs](https://docs.rs/http_req/badge.svg)](https://docs.rs/http_req/0.9.3/http_req/)
[![Crates.io](https://img.shields.io/badge/crates.io-v0.10.0-orange.svg?longCache=true)](https://crates.io/crates/http_req)
[![Docs.rs](https://docs.rs/http_req/badge.svg)](https://docs.rs/http_req/0.10.0/http_req/)

Simple and lightweight HTTP client with built-in HTTPS support.

## Requirements
http_req by default uses [rust-native-tls](https://github.com/sfackler/rust-native-tls),
which uses TLS framework provided by OS on Windows and macOS, and OpenSSL
which relies on TLS framework provided by OS on Windows and macOS, and OpenSSL
on all other platforms. But it also supports [rus-tls](https://crates.io/crates/rustls).

## Example
Basic GET request
Basic HTTP GET request
```rust
use http_req::request;

fn main() {
let mut writer = Vec::new(); //container for body of a response
let res = request::get("https://doc.rust-lang.org/", &mut writer).unwrap();
let mut body = Vec::new(); //Container for body of a response.
let res = request::get("https://doc.rust-lang.org/", &mut body).unwrap();

println!("Status: {} {}", res.status_code(), res.reason());
}
```

Take a look at [more examples](https://github.com/jayjamesjay/http_req/tree/master/examples)

## How to use with `rustls`:
In order to use `http_req` with `rustls` in your project, add following lines to `Cargo.toml`:
```toml
[dependencies]
http_req = {version="^0.9", default-features = false, features = ["rust-tls"]}
http_req = {version="^0.10", default-features = false, features = ["rust-tls"]}
```

## License
Expand Down
12 changes: 8 additions & 4 deletions examples/get.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use http_req::request;

fn main() {
let mut writer = Vec::new(); //container for body of a response
let res = request::get("https://www.rust-lang.org/learn", &mut writer).unwrap();
//Container for body of a response.
let mut body = Vec::new();

//Sends a HTTP GET request and processes the response. Saves body of the response to `body` variable.
let res = request::get("https://www.rust-lang.org/learn", &mut body).unwrap();

//Prints details about the response.
println!("Status: {} {}", res.status_code(), res.reason());
println!("Headers {}", res.headers());
//println!("{}", String::from_utf8_lossy(&writer));
println!("Headers: {}", res.headers());
//println!("{}", String::from_utf8_lossy(&body));
}
4 changes: 3 additions & 1 deletion examples/head.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use http_req::request;

fn main() {
//Sends a HTTP HEAD request and processes the response.
let res = request::head("https://www.rust-lang.org/learn").unwrap();

//Prints details about the response.
println!("Status: {} {}", res.status_code(), res.reason());
println!("{:?}", res.headers());
println!("Headers: {}", res.headers());
}
16 changes: 11 additions & 5 deletions examples/post.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use http_req::request;

fn main() {
let mut writer = Vec::new(); //container for body of a response
const BODY: &[u8; 27] = b"field1=value1&field2=value2";
let res = request::post("https://httpbin.org/post", BODY, &mut writer).unwrap();
//Container for body of a response.
let mut res_body = Vec::new();

//Body of a request.
const REQ_BODY: &[u8; 27] = b"field1=value1&field2=value2";

//Sends a HTTP POST request and processes the response.
let res = request::post("https://httpbin.org/post", REQ_BODY, &mut res_body).unwrap();

//Prints details about the response.
println!("Status: {} {}", res.status_code(), res.reason());
println!("Headers {}", res.headers());
//println!("{}", String::from_utf8_lossy(&writer));
println!("Headers: {}", res.headers());
println!("{}", String::from_utf8_lossy(&res_body));
}
13 changes: 7 additions & 6 deletions examples/request_builder_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ use http_req::{request::RequestBuilder, tls, uri::Uri};
use std::{convert::TryFrom, net::TcpStream};

fn main() {
//Parse uri and assign it to variable `addr`
let addr: Uri = Uri::try_from("https://doc.rust-lang.org/").unwrap();
//Parses a URI and assigns it to a variable `addr`.
let addr: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();

//Connect to remote host
//Connects to a remote host. Uses information from `addr`.
let stream = TcpStream::connect((addr.host().unwrap(), addr.corr_port())).unwrap();

//Open secure connection over TlsStream, because of `addr` (https)
//Opens a secure connection over TlsStream. This is required due to use of `https` protocol.
let mut stream = tls::Config::default()
.connect(addr.host().unwrap_or(""), stream)
.unwrap();

//Container for response's body
//Container for a response's body.
let mut writer = Vec::new();

//Add header `Connection: Close`
//Adds a header `Connection: Close`.
let response = RequestBuilder::new(&addr)
.header("Connection", "Close")
.send(&mut stream, &mut writer)
.unwrap();

println!("Status: {} {}", response.status_code(), response.reason());
println!("Headers: {}", response.headers());
//println!("{}", String::from_utf8_lossy(&writer));
}
13 changes: 13 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum ParseErr {
UriErr,
Invalid,
Empty,
#[cfg(feature = "rust-tls")]
Rustls(rustls::Error),
}

impl error::Error for ParseErr {
Expand All @@ -20,6 +22,8 @@ impl error::Error for ParseErr {
Utf8(e) => Some(e),
Int(e) => Some(e),
StatusErr | HeadersErr | UriErr | Invalid | Empty => None,
#[cfg(feature = "rust-tls")]
Rustls(e) => Some(e),
}
}
}
Expand All @@ -36,11 +40,20 @@ impl fmt::Display for ParseErr {
StatusErr => "status line contains invalid values",
HeadersErr => "headers contain invalid values",
UriErr => "uri contains invalid characters",
#[cfg(feature = "rust-tls")]
Rustls(_) => "rustls error",
};
write!(f, "ParseErr: {}", err)
}
}

#[cfg(feature = "rust-tls")]
impl From<rustls::Error> for ParseErr {
fn from(e: rustls::Error) -> Self {
ParseErr::Rustls(e)
}
}

impl From<num::ParseIntError> for ParseErr {
fn from(e: num::ParseIntError) -> Self {
ParseErr::Int(e)
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
//!use http_req::request;
//!
//!fn main() {
//! let mut writer = Vec::new(); //container for body of a response
//! let res = request::get("https://doc.rust-lang.org/", &mut writer).unwrap();
//! //Container for body of a response
//! let mut body = Vec::new();
//! let res = request::get("https://doc.rust-lang.org/", &mut body).unwrap();
//!
//! println!("Status: {} {}", res.status_code(), res.reason());
//!}
Expand Down
Loading