-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Response method body_string may use wrong character encoding #101
Comments
@Hawk777 Thanks for filing this; I hear you. I think I built it this way because Rust strings are always guaranteed to be valid utf8. But if the encoding of the incoming request is different we should probably convert it first. Does that sound like it would be the right way to go? Additionally we should probably indeed update the docs with more details of what we're doing. I hadn't really thought of it, but now that you mention it it's not surprising it can be confusing to people. Apologies! |
Yes, that is exactly what I would expect. It might also be worth deciding and documenting what the behaviour is (panic? return error?) if the response body is not actually text (e.g. |
I think an Use Content-Type or fall back exampleCould be something like: let response = surf::get("https://some.website/whatever").await?;
let body = match response.body_string().await {
Ok(body) => body,
Err(BodyStringError::DecodeError(bytes)) => try_detect_decode(bytes)?,
Err(err) => Err(err)?,
};
do_something_with(body);
fn try_detect_decode(bytes: Vec<u8>) -> Result<String, SomeError> {
let (encoding_name, _confidence) = chardet::detect(&bytes);
if let Some(encoding) = encoding_rs::Encoding::for_label(encoding_name) {
let (decoded, _used_encoding, failed) = encoding.decode(&bytes);
if failed {
Err("could not decode body")
} else {
Ok(decoded.to_string())
}
} else {
return Err("could not detect encoding")
}
}
|
Should be addressed by #108! It tries to use the encoding from the |
The
body_string
method in theResponse
class is as follows:This just grabs the body as a byte array, and then decodes it with UTF-8. If it wasn’t originally encoded in UTF-8, this is wrong. What I think most people would expect
body_string
to do (I certainly did) is extract the character encoding from thecharset
parameter of theContent-Type
header and decode the bytes to a string using that encoding.While there is a note that “If the body cannot be interpreted as valid UTF-8, an
Err
is returned.” I found this confusing. I didn’t think it meant that the declared encoding was ignored, I thought it actually meant if the body could not be represented in UTF-8 (perhaps because it was binary data not matching any, or the declared, encoding). I am not sure whether this method, as currently written, is really all that useful? I found it rather misleading myself.The text was updated successfully, but these errors were encountered: