-
-
Save marceloneppel/aace980add2570064a0b47ac0bef7607 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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);