Skip to content

Instantly share code, notes, and snippets.

@michielmulders
Created September 11, 2020 09:54
Show Gist options
  • Save michielmulders/ac274cc09af94afad4ab1a0da2a9f4c9 to your computer and use it in GitHub Desktop.
Save michielmulders/ac274cc09af94afad4ab1a0da2a9f4c9 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();
}
@BH1SCW
Copy link

BH1SCW commented Aug 30, 2022

so, how can I print response message? Thanks

@djve60
Copy link

djve60 commented Sep 19, 2022

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