ducaale/xh · error

is not a supported encoding, please refer to…

Error message

{} is not a supported encoding, please refer to https://encoding.spec.whatwg.org/#names-and-labels for supported encodings

What it means

The --encoding argument is validated against the WHATWG Encoding Standard via encoding_rs::Encoding::for_label. If the label doesn't resolve to a known encoding, the parser rejects it and points at the official label list.

Solutions

  1. Use a WHATWG label exactly, e.g. --encoding utf-8, windows-1252, or iso-8859-1
  2. Check the linked spec page https://encoding.spec.whatwg.org/#names-and-labels for valid labels
  3. Common aliases: use 'latin1' or 'iso-8859-1' instead of 'latin0'; 'gb18030' instead of Chinese variants not in the spec

Example fix

// before
xh --encoding utf8mb4 GET https://example.com
// after
xh --encoding utf-8 GET https://example.com
Defensive patterns

Strategy: validation

Validate before calling

fn is_valid_encoding_label(label: &str) -> bool {
    encoding_rs::Encoding::for_label(label.as_bytes()).is_some()
}

Type guard

fn valid_encoding(label: &str) -> Option<&'static encoding_rs::Encoding> {
    encoding_rs::Encoding::for_label(label.as_bytes())
}

Try / catch

match parse_encoding(&label) {
    Ok(enc) => enc,
    Err(e) => eprintln!("{e}; use labels like utf-8, windows-1252, iso-8859-1"),
}

Prevention

When it happens

Trigger: Passing --encoding utf8 (not a valid label alone in some contexts), --encoding ascii-8bit, --encoding latin0, or any misspelled/unregistered encoding name.

Common situations: Using aliases from other languages (e.g. Python's 'utf-8' works but invented names like 'utf8mb4' or 'win-1252x' do not), assuming all iconv names are supported.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of ducaale/xh@2404aceecc (2026-09-13). Data as JSON: /api/errors/4d13b595f0b59367. Report an issue: GitHub.

Appendix: source

Thrown at src/cli.rs:1414

        &normalized_encoding.replace('_', "-"),
        &normalized_encoding.replace('-', "_"),
    ] {
        if let Some(encoding) = Encoding::for_label(encoding.as_bytes()) {
            return Ok(encoding);
        }
    }

    {
        let mut encoding = normalized_encoding.replace(&['-', '_'][..], "");
        if let Some(first_digit_index) = encoding.find(|c: char| c.is_ascii_digit()) {
            encoding.insert(first_digit_index, '-');
            if let Some(encoding) = Encoding::for_label(encoding.as_bytes()) {
                return Ok(encoding);
            }
        }
    }

    Err(anyhow::anyhow!(
        "{} is not a supported encoding, please refer to https://encoding.spec.whatwg.org/#names-and-labels \
         for supported encodings",
        encoding
    ))
}

/// Based on the function used by clap to abort
fn safe_exit() -> ! {
    let _ = std::io::stdout().lock().flush();
    let _ = std::io::stderr().lock().flush();
    std::process::exit(0);
}

fn long_version() -> &'static str {
    concat!(env!("CARGO_PKG_VERSION"), "\n", env!("XH_FEATURES"))
}

#[cfg(test)]

View on GitHub (pinned to 2404aceecc)