How do I make an HTTP request from Rust?

前端 未结 7 889
长情又很酷
长情又很酷 2020-12-02 12:04

How can I make an HTTP request from Rust? I can\'t seem to find anything in the core library.

I don\'t need to parse the output, just make a request and check the HT

7条回答
  •  执念已碎
    2020-12-02 12:50

    Using curl bindings. Stick this in your Cargo.toml:

    [dependencies.curl]
    git = "https://github.com/carllerche/curl-rust"
    

    ...and this in the src/main.rs:

    extern crate curl;
    
    use curl::http;
    
    fn main(){
      let resp = http::handle()
        .post("http://localhost:3000/login", "username=dude&password=sikrit")
        .exec().unwrap();
    
      println!("code={}; headers={}; body={}",
        resp.get_code(), resp.get_headers(), resp.get_body());    
    
    }
    

提交回复
热议问题