gleam-lang/gleam · error

Unable to convert HTTP request for use by reqwest library

Error message

Unable to convert HTTP request for use by reqwest library

What it means

This panic occurs in the CLI's HTTP send helper when a gleam_core::http::Request cannot be converted into a native reqwest::Request via TryInto. The conversion is total for all well-formed requests the compiler builds, so a failure indicates an internal invariant violation (e.g. invalid URL or unsupported method/version combination reaching reqwest). The .expect() deliberately crashes rather than sending a malformed request.

Solutions

  1. Check the package repository URL configured for the command (gleam.toml repositories / GLEAM_ env vars) is a well-formed absolute HTTP(S) URL
  2. Update gleam to the latest version, as conversion failures usually indicate a compiler bug in request construction
  3. Reproduce with RUST_BACKTRACE=1 and file a Gleam issue with the failing command and URL
  4. Work around by fixing network config (HTTPS_PROXY etc.) if a proxy setting is mangling the URL

Example fix

// before (misconfigured repo URL)
// gleam.toml: [dependencies] my_pkg = { path = "../my_pkg" } with repo URL "myrepo"
// after
// use a fully qualified URL the reqwest builder accepts:
// repository = "https://hex.pm/packages"
Defensive patterns

Strategy: validation

Validate before calling

fn assert_valid_request(req: &gleam_core::http::Request) -> Result<(), String> {
    let url = req.uri().to_string();
    if url.parse::<reqwest::Url>().is_err() {
        return Err(format!("request URL not parseable: {}", url));
    }
    Ok(())
}

Type guard

fn is_http_url(s: &str) -> bool {
    s.starts_with("http://") || s.starts_with("https://")
}

Prevention

When it happens

Trigger: Any code path that calls send() — remove, revert, and authenticate commands, plus package downloads — when the request's URI or fields fail reqwest's request builder validation (e.g. a URL reqwest rejects, or a method/header that cannot be represented).

Common situations: An unreachable or malformed registry/base URL from a misconfigured package repository in gleam.toml or environment; a corrupted internal request construction after a gleam/gleam-core version mismatch; proxy environment variables producing URLs reqwest cannot parse.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


AI-assisted analysis of gleam-lang/gleam@15b07c7830 (2026-09-14). Data as JSON: /api/errors/897a2794b836358b. Report an issue: GitHub.

Appendix: source

Thrown at compiler-cli/src/http.rs:43

        Self
    }

    pub fn boxed() -> Box<Self> {
        Box::new(Self::new())
    }
}

#[async_trait]
impl gleam_core::io::HttpClient for HttpClient {
    async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Vec<u8>>> {
        tracing::debug!(
            method = request.method().as_str(),
            url = request.uri().to_string(),
            "http-send",
        );
        let request = request
            .try_into()
            .expect("Unable to convert HTTP request for use by reqwest library");
        let client = init_client().map_err(Error::http)?;
        let mut response = client.execute(request).await.map_err(Error::http)?;
        let mut builder = Response::builder()
            .status(response.status())
            .version(response.version());
        if let Some(headers) = builder.headers_mut() {
            std::mem::swap(headers, response.headers_mut());
        }
        builder
            .body(response.bytes().await.map_err(Error::http)?.to_vec())
            .map_err(Error::http)
    }
}

fn init_client() -> Result<&'static Client, Error> {
    if let Some(client) = REQWEST_CLIENT.get() {
        return Ok(client);
    }

View on GitHub (pinned to 15b07c7830)