Created
September 11, 2020 09:54
-
-
Save michielmulders/ac274cc09af94afad4ab1a0da2a9f4c9 to your computer and use it in GitHub Desktop.
Curl-rust POST request example
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
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(); | |
} |
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
so, how can I print response message? Thanks