Created
July 18, 2018 06:57
-
-
Save fliar/cf93aa56b9602d5b3e9a84d353c57227 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "hyper02" | |
version = "0.1.0" | |
authors = ["fliar"] | |
[dependencies] | |
hyper-tls = "0.3.0" | |
futures = "0.1.22" | |
tokio = "0.1" | |
hyper = "0.12" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate futures; | |
extern crate hyper; | |
extern crate hyper_tls; | |
extern crate tokio; | |
use futures::{future, Future}; | |
use hyper::header::CONTENT_TYPE; | |
use hyper::rt::Stream; | |
use hyper::{Body, Client, Method, Request}; | |
use hyper_tls::HttpsConnector; | |
use std::fs::File; | |
use std::io::prelude::*; | |
use std::io::{self, Write}; | |
const BOUNDARY: &'static str = "------------------------ea3bbcf87c101592"; | |
fn main() { | |
tokio::run(future::lazy(|| { | |
let https = HttpsConnector::new(4).unwrap(); | |
let client = Client::builder().build::<_, hyper::Body>(https); | |
let mut req = Request::new(Body::from(image_data())); | |
req.headers_mut().insert( | |
CONTENT_TYPE, | |
format!("multipart/form-data; boundary={}", BOUNDARY) | |
.parse() | |
.unwrap(), | |
); | |
*req.method_mut() = Method::POST; | |
*req.uri_mut() = "https://httpbin.org/post".parse().unwrap(); | |
client | |
.request(req) | |
.and_then(|res| { | |
println!("status: {}", res.status()); | |
res.into_body().for_each(|chunk| { | |
io::stdout() | |
.write_all(&chunk) | |
.map_err(|e| panic!("stdout error: {}", e)) | |
}) | |
}) | |
.map_err(|e| println!("request error: {}", e)) | |
})); | |
} | |
fn image_data() -> Vec<u8> { | |
let mut result: Vec<u8> = Vec::new(); | |
result.extend_from_slice(format!("--{}\r\n", BOUNDARY).as_bytes()); | |
result.extend_from_slice(format!("Content-Disposition: form-data; name=\"text\"\r\n").as_bytes()); | |
result.extend_from_slice("title\r\n".as_bytes()); | |
result.extend_from_slice(format!("--{}\r\n", BOUNDARY).as_bytes()); | |
result.extend_from_slice(format!("Content-Disposition: form-data; name=\"smfile\"; filename=\"11.jpg\"\r\n").as_bytes()); | |
result.extend_from_slice("Content-Type: image/jpeg\r\n\r\n".as_bytes()); | |
let mut f = File::open("11.jpg").unwrap(); | |
let mut file_data = Vec::new(); | |
f.read_to_end(&mut file_data).unwrap(); | |
result.append(&mut file_data); | |
result.extend_from_slice(format!("--{}--\r\n", BOUNDARY).as_bytes()); | |
result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment