Skip to content

Instantly share code, notes, and snippets.

@odra
Created February 10, 2020 18:55
Show Gist options
  • Save odra/8829a082e828454639f5f6a302aac634 to your computer and use it in GitHub Desktop.
Save odra/8829a082e828454639f5f6a302aac634 to your computer and use it in GitHub Desktop.
rust http client
use std::io::prelude::*;
use std::net::TcpStream;
fn main() {
let mut b = vec![0; 4096];
let mut s = String::new();
let headers = "GET /ip HTTP/1.1\r\nHost: httpbin.org\r\nUser-Agent: foobar/0.0.1\r\nAccept: */*\r\n\r\n";
let mut stream = TcpStream::connect("httpbin.org:80").unwrap();
stream.write(headers.as_bytes()).unwrap();
loop {
match stream.read(&mut b) {
Ok(n) => {
if n == 0 {
break;
}
let text = std::str::from_utf8(&b).unwrap();
s.push_str(text);
},
Err(e) => panic!(e)
}
}
print!("{}", s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment