Skip to content

Latest commit

 

History

History
43 lines (36 loc) · 1.61 KB

post-file.zh.md

File metadata and controls

43 lines (36 loc) · 1.61 KB

POST 一个文件,到 paste.rs

[![reqwest-badge]][reqwest] [![cat-net-badge]][cat-net]

reqwest::Client建立起到 https://paste.rs 的一个连接,遵循着reqwest::RequestBuilder模式。调用Client::post,加上 目标 URL 参数,RequestBuilder::body通过读取文件,设置要发送的内容,以及RequestBuilder::send会堵塞,直到文件上载并返回响应。read_to_string返回响应,并显示在控制台中。

extern crate reqwest;

# #[macro_use]
# extern crate error_chain;
#
use std::fs::File;
use std::io::Read;
use reqwest::Client;
#
# error_chain! {
#     foreign_links {
#         HttpRequest(reqwest::Error);
#         IoError(::std::io::Error);
#     }
# }

fn run() -> Result<()> {
    let paste_api = "https://paste.rs";
    let file = File::open("message")?;

    let mut response = Client::new().post(paste_api).body(file).send()?;
    let mut response_body = String::new();
    response.read_to_string(&mut response_body)?;
    println!("Your paste is located at: {}", response_body);
    Ok(())
}
#
# quick_main!(run);