Skip to content

Instantly share code, notes, and snippets.

@dacr
Created November 17, 2024 18:09
Show Gist options
  • Save dacr/814b774ca0e7589112c3e5896d45c5d0 to your computer and use it in GitHub Desktop.
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/b7565908e2b358fd4f356aac538ea3b89d6a32
#!/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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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
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