LoginSignup
2
1

More than 5 years have passed since last update.

Rustでmultipart request

Last updated at Posted at 2017-12-17

ファイルのアップロードを行います

# Cargo.toml
reqwest = "~0.8.1"
let client = reqwest::Client::new();

let form = reqwest::multipart::Form::new()
    .file("file", "temp.txt")
    .expect("Bro, your 'temp.txt' can't be opened");

let mut res = client.post("url")
    .multipart(form)
    .send()
    .unwrap();

res.copy_to(&mut std::io::stdout()).unwrap();

おまけ ファイルをバイナリに埋め込む謎ユースケース

let file_content = include_str!("temp.txt");

let client = reqwest::Client::new();

let part = reqwest::multipart::Part::text(file_content)
    .file_name("temp.txt")
    .mime(reqwest::mime::TEXT_PLAIN);    

let form = reqwest::multipart::Form::new()
    .part("file", part);

// ここからはおなじ
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1