Skip to content

Instantly share code, notes, and snippets.

@marceloneppel
Forked from michielmulders/curl-rust.rs
Created April 12, 2025 16:54
Show Gist options
  • Save marceloneppel/aace980add2570064a0b47ac0bef7607 to your computer and use it in GitHub Desktop.
Save marceloneppel/aace980add2570064a0b47ac0bef7607 to your computer and use it in GitHub Desktop.
Curl-rust POST request example
use std::io::Read;
use curl::easy::Easy;
fn main() {
let mut data = "this is the body".as_bytes();
let mut easy = Easy::new();
easy.url("http://www.example.com/upload").unwrap();
easy.post(true).unwrap();
easy.post_field_size(data.len() as u64).unwrap();
let mut transfer = easy.transfer();
transfer.read_function(|buf| {
Ok(data.read(buf).unwrap_or(0))
}).unwrap();
transfer.perform().unwrap();
}
@marceloneppel
Copy link
Author

From https://gist.github.com/michielmulders/ac274cc09af94afad4ab1a0da2a9f4c9?permalink_comment_id=4308317#gistcomment-4308317:

Before the perform and after the transfer.read_function() insert:

transfer.write_function(|data| {
html_data = String::from_utf8(Vec::from(data)).unwrap();
Ok(data.len())
}).unwrap();

and after the perform:

println!("HTML body {:#?}", html_data);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment