Skip to content

Creating a request with Body::empty() sends a request with Body(0) #452

Open
@JoseSFOc

Description

isahc = "1.7.2"
rustc 1.79.0 (129f3b996 2024-06-10)
isahc/1.7.2 (features:default,encoding-rs,http2,mime,static-curl,text-decoding) libcurl/8.8.0-DEV SecureTransport zlib/1.2.12 nghttp2/1.61.0

Hello!

My team is currently working on a system that has to interact with many different APIs, we migrated from reqwest for some issues with the library that were solved by isahc.

We've now encountered a problem where some APIs don't accept a request with an empty body as it would do with no body at all. We're creating the request using the following method:

fn set_body(
    builder: isahc::http::request::Builder,
    body: Option<serde_json::Value>,
) -> Result<isahc::Request<Body>, Error> {
    if let Some(body) = body {
        let body_str = body.as_str().unwrap_or_default();
        builder.body(body_str.into()).map_err(Error::IsahcHttpError)
    } else {
        builder.body(Body::empty()).map_err(Error::IsahcHttpError)
    }
}

As the documentation says that Body::empty() gives a request with the absence of a body -> Body::empty()

But when displaying the log information for the created request we obtain this (obscuring some sensible information):

Request { method: POST, uri: ***, version: HTTP/1.1, headers: {"x-api-key": "***", "x-nonce": "1720526846267", "api-signature": "***"}, body: Body(0) }

While reqwest generates the following:

RequestBuilder { method: POST, url: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("***")), port: None, path: "***", query: None, fragment: None }, headers: {"x-api-key": "***", "x-nonce": "1720525011723", "api-signature": "***"} }

As in the previous example, using Body::empty() is creating a body in the request with length zero, is there any other way to create a request with no body? Using Body::empty() should create a empty body instead of no body in the request?

Thanks!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions