16dbb5987Sopenharmony_ci// Copyright (c) 2023 Huawei Device Co., Ltd. 26dbb5987Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License"); 36dbb5987Sopenharmony_ci// you may not use this file except in compliance with the License. 46dbb5987Sopenharmony_ci// You may obtain a copy of the License at 56dbb5987Sopenharmony_ci// 66dbb5987Sopenharmony_ci// http://www.apache.org/licenses/LICENSE-2.0 76dbb5987Sopenharmony_ci// 86dbb5987Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software 96dbb5987Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS, 106dbb5987Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 116dbb5987Sopenharmony_ci// See the License for the specific language governing permissions and 126dbb5987Sopenharmony_ci// limitations under the License. 136dbb5987Sopenharmony_ci 146dbb5987Sopenharmony_ci//! This is a simple synchronous HTTP client example using the ylong_http_client 156dbb5987Sopenharmony_ci//! crate. It demonstrates creating a client, making a request, and reading the 166dbb5987Sopenharmony_ci//! response. 176dbb5987Sopenharmony_ci 186dbb5987Sopenharmony_ciuse ylong_http_client::sync_impl::{BodyReader, Client}; 196dbb5987Sopenharmony_ciuse ylong_http_client::{HttpClientError, Request}; 206dbb5987Sopenharmony_ci 216dbb5987Sopenharmony_cifn main() -> Result<(), HttpClientError> { 226dbb5987Sopenharmony_ci // Creates a `sync_impl::Client` 236dbb5987Sopenharmony_ci let client = Client::new(); 246dbb5987Sopenharmony_ci 256dbb5987Sopenharmony_ci // Creates a `Request`. 266dbb5987Sopenharmony_ci let request = Request::get("127.0.0.1:3000") 276dbb5987Sopenharmony_ci .body("".as_bytes()) 286dbb5987Sopenharmony_ci .map_err(HttpClientError::other)?; 296dbb5987Sopenharmony_ci 306dbb5987Sopenharmony_ci // Sends request and receives a `Response`. 316dbb5987Sopenharmony_ci let mut response = client.request(request)?; 326dbb5987Sopenharmony_ci 336dbb5987Sopenharmony_ci // Reads the body of `Response` by using `BodyReader`. 346dbb5987Sopenharmony_ci let _ = BodyReader::default().read_all(response.body_mut()); 356dbb5987Sopenharmony_ci Ok(()) 366dbb5987Sopenharmony_ci} 37