diff --git a/hitt-request/src/lib.rs b/hitt-request/src/lib.rs index bd7514e..a512b15 100644 --- a/hitt-request/src/lib.rs +++ b/hitt-request/src/lib.rs @@ -15,34 +15,37 @@ pub async fn send_request( input: &HittRequest, ) -> Result { let url = input.uri.to_string(); - let method = input.method.to_string(); - let req = http_client - .request(input.method.clone(), &url) - .headers(input.headers.clone()) - .body(input.body.clone().unwrap_or_default()) - .version(input.http_version.unwrap_or(reqwest::Version::HTTP_11)) - .build()?; + let mut partial_req = http_client.request(input.method.clone(), &url); + + if let Some(http_version) = input.http_version { + partial_req = partial_req.version(http_version); + } + + if !input.headers.is_empty() { + partial_req = partial_req.headers(input.headers.clone()); + } + + if input.body.is_some() { + if let Some(body) = input.body.clone() { + partial_req = partial_req.body(body); + } + } + + let req = partial_req.build()?; // TODO: implement more precise benchmark? let start = std::time::Instant::now(); let res = http_client.execute(req).await?; let duration = start.elapsed(); - let status_code = res.status(); - let headers = res.headers().to_owned(); - - let http_version = res.version(); - - let body = res.text().await.unwrap_or_default(); - Ok(HittResponse { url, - method, - status_code, - headers, - body, - http_version, + method: input.method.to_string(), + status_code: res.status(), + headers: res.headers().to_owned(), + http_version: res.version(), duration, + body: res.text().await.unwrap_or_default(), }) }