Last active
March 2, 2018 00:14
-
-
Save snobu/1d3dce6ce303907fee4a8f4529ce8b27 to your computer and use it in GitHub Desktop.
web-request-rust.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://chr4.org/blog/2017/03/15/cross-compile-and-link-a-static-binary-on-macos-for-linux-with-cargo-and-rust/ | |
| // | |
| // cargo new projectname | |
| // | |
| // Cargo.toml: | |
| // | |
| // [package] | |
| // name = "webreq" | |
| // version = "0.1.0" | |
| // authors = ["snobu <foo@xxxxx.zzz>"] | |
| // | |
| // [dependencies] | |
| // reqwest = "0.8.5" | |
| // error-chain = "0.11.0" | |
| // env_logger = "0.5.4" | |
| // | |
| // rustup target add x86_64-unknown-linux-musl | |
| // apt install musl-tools | |
| // cargo build --release --target x86_64-unknown-linux-musl | |
| extern crate reqwest; | |
| extern crate env_logger; | |
| #[macro_use] | |
| extern crate error_chain; | |
| error_chain! { | |
| foreign_links { | |
| ReqError(reqwest::Error); | |
| IoError(std::io::Error); | |
| } | |
| } | |
| fn run() -> Result<()> { | |
| env_logger::init(); | |
| println!("GET https://www.rust-lang.org"); | |
| let mut res = reqwest::get("https://jsonplaceholder.typicode.com/posts/1")?; | |
| println!("Status: {}", res.status()); | |
| // println!("Headers:\n{}", res.headers()); | |
| // copy the response body directly to stdout | |
| let _ = std::io::copy(&mut res, &mut std::io::stdout())?; | |
| println!("\n\nDone."); | |
| Ok(()) | |
| } | |
| quick_main!(run); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment