How do I make an HTTP request from Rust?

前端 未结 7 855
长情又很酷
长情又很酷 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:45

    I prefer Crates with low dependency count, so I would recommend these:

    MinReq (0 deps)

    use minreq;
    
    fn main() -> Result<(), minreq::Error> {
       let o = minreq::get("https://speedtest.lax.hivelocity.net").send()?;
       let s = o.as_str()?;
       print!("{}", s);
       Ok(())
    }
    

    HTTP_Req (35 deps)

    use {http_req::error, http_req::request, std::io, std::io::Write};
    
    fn main() -> Result<(), error::Error> {
       let mut a = Vec::new();
       request::get("https://speedtest.lax.hivelocity.net", &mut a)?;
       io::stdout().write(&a)?;
       Ok(())
    }
    

提交回复
热议问题