1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 //! Uses an Asynchronous HTTP client to send a `POST` request with multipart body.
17
18 use ylong_http_client::async_impl::{Client, Downloader, MultiPart, Part, Uploader};
19 use ylong_http_client::{Method, Request};
20
21 #[tokio::main]
main()22 async fn main() {
23 // Customizes your HTTP client.
24 let client = Client::builder().build().unwrap();
25
26 // Customize your `Multipart` messages.
27 let multipart = MultiPart::new()
28 .part(Part::new().name("name").body("xiaoming")) // Adds your parts.
29 .part(Part::new().name("password").body("123456789"))
30 .part(
31 Part::new()
32 .name("123")
33 .length(Some(10))
34 .stream("HelloWorld".as_bytes()),
35 );
36
37 // Uses `Uploader` to upload the `Multipart` with progress message displayed on console.
38 let uploader = Uploader::builder().multipart(multipart).console().build();
39
40 // Customizes your HTTP request.
41 let request = Request::builder()
42 .method(Method::POST)
43 .url("http://www.example.com")
44 .multipart(uploader) // Sets the multipart body.
45 .unwrap();
46
47 // Sends your HTTP request through the client.
48 let response = client.request(request).await.unwrap();
49
50 // Uses `Downloader` to download the response body and display message on console.
51 let _ = Downloader::console(response).download().await;
52 }
53