Last active
February 3, 2026 20:23
-
-
Save dacr/814b774ca0e7589112c3e5896d45c5d0 to your computer and use it in GitHub Desktop.
hello rust http client / published by https://github.com/dacr/code-examples-manager #2455a322-c6a7-4c96-834e-262b3917262c/4ac8c3ee756c8ae597ee7c982d22f1cf65c9b8a8
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
| #!/usr/bin/env rust-script | |
| // cargo-deps : openssl="0.10.68", openssl-sys="0.9.104", curl="0.4.47" | |
| // summary : hello rust http client | |
| // keywords : rust, http-client, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 2455a322-c6a7-4c96-834e-262b3917262c | |
| // created-on : 2024-10-25T19:09:32+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : ./$file | |
| //// REQUIREMENTS : | |
| //// sudo apt-get install libssl-dev | |
| //// nix-shell -p openssl | |
| use curl::easy::Easy; | |
| use std::str::from_utf8; | |
| fn main() { | |
| let mut dst = Vec::new(); | |
| let mut query = Easy::new(); | |
| query.url("https://httpbin.org/get").unwrap(); | |
| { | |
| let mut transfer = query.transfer(); | |
| transfer.write_function(|data| { | |
| dst.extend_from_slice(data); | |
| Ok(data.len()) | |
| }).unwrap(); | |
| transfer.perform().unwrap(); | |
| } | |
| println!("received={} bytes", dst.len()); | |
| from_utf8(dst.as_slice()) | |
| .iter().for_each(|s| println!("{}", s)); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment