You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
programming-rust-examples/http-get/src/main.rs

31 lines
764 B
Rust

extern crate reqwest;
use std::error::Error;
use std::io::{self, Write};
fn http_get_main(url: &str) -> Result<(), Box<Error>> {
// Send the HTTP request and get a response.
let mut response = reqwest::get(url)?;
if !response.status().is_success() {
Err(format!("{}", response.status()))?;
}
// Read the response body and write it to stdout.
let stdout = io::stdout();
io::copy(&mut response, &mut stdout.lock())?;
Ok(())
}
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() != 2 {
writeln!(io::stderr(), "usage: http-get URL").unwrap();
return;
}
if let Err(err) = http_get_main(&args[1]) {
writeln!(io::stderr(), "error: {}", err).unwrap();
}
}