Repository navigation
Fix http response body error message for some content-type - #19121
Conversation
| let msg = if response.header("content-type") == Some("application/json") { | ||
| let msg = if response | ||
| .header("content-type") | ||
| .is_some_and(|s| s.starts_with("application/json")) |
There was a problem hiding this comment.
As you mentioned about charset=utf-8, maybe it's bettwe to check if it's utf-8, but we can assume the default value is that. But if it says explicitly like charset=gbk, we shouldn't try to decode it to String
There was a problem hiding this comment.
Fair point. I went for matching exactly two cases: charset=utf-8, and no charset specified.
dc55470 to
ef66131
Compare
|
please try and get the ci green |
ef66131 to
95ee8d6
Compare
|
Oh, sorry. Fixed. |
fdncred
left a comment
There was a problem hiding this comment.
Thanks for the fix. I left inline notes on the matching logic. Two more things that aren't on diff lines:
No test covers this branch. check_response_status returns Ok when --allow-errors is set, so http_get_with_accept_errors_and_full_json_response never reaches handle_status_error. The existing *_failed_due_to_server_error tests only check for "Bad Request" without a JSON content type. No test exercised the JSON-body path before this PR either, so a regression here would go unnoticed. Something like this in crates/nu-command/tests/commands/network/http/get.rs, modeled on http_get_failed_due_to_server_error:
let _mock = server
.mock("GET", "/")
.with_status(400)
.with_header("content-type", "application/json; charset=utf-8")
.with_body(r#"{"msg": "boom"}"#)
.create();
// ... expect_shell_error() -> ShellError::HttpError { msg, .. } => assert_contains("boom", msg)A application/problem+json case and a application/json;charset=UTF-8 (no space) case would cover the inline notes.
Minor, in unchanged code in the same function: .unwrap_or("Cannot read body".to_string()) allocates even when the read succeeds (unwrap_or_else avoids that). When the read does fail (body over ureq's read limit, or bytes that aren't valid UTF-8), the message becomes "Cannot read body" and the status's canonical reason is lost. Falling back to canonical_reason() would keep the error informative.
|
where are we at on this? |
95ee8d6 to
2ff9e69
Compare
|
Oh, sorry, I left this hanging. I've just pushed a commit that reworks the json detection using the helper you provided (no change in |
The content-type check was naive and would not handle the case of the header having a charset, like in "application/json; charset=utf-8". Change-Id: If34b90c637e69b056f3708d753ff4cba6a6a6964
- better json detection in a helper - avoid unwrap_or - an actual test Change-Id: I46f617ea9787a8a9a9b0ced0da077e2c6a6a6964
2ff9e69 to
6c54c5b
Compare
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
Three of the four threads are fixed and resolved: +json types, the case and parameter spellings, and the shared is_json_content_type helper. WindSoilder's thread about explicit non-UTF-8 charsets stays open. One new problem: the wider match now blanks the error label when a JSON error response has an empty body, which every http head response has.
Findings: 0
Notes from my clanker, checked on 6c54c5b. Built the branch and ran cargo test -p nu-command --test tests -- commands::network::http::get (26 passed, 4 ignored). Ran 400/404/405/500 responses with each content type against a local server on the branch binary and on 0.117.1 (7bff3d1, same http code as main). Built the suggested fallback in the PR worktree: the empty-body cases print the reason phrase again, and all 154 commands::network::http tests pass.
| response | ||
| .body_mut() | ||
| .read_to_string() | ||
| .unwrap_or("Cannot read body".to_string()) | ||
| .unwrap_or_else(|_| "Cannot read body".to_string()) |
There was a problem hiding this comment.
handle_status_error also handles http head and http options (head.rs:232, options.rs:229). A HEAD response never has a body, so read_to_string() returns Ok("") and the label is empty. Main only did this for a bare application/json. With the wider match it now happens for application/json; charset=utf-8, which Express sends by default, and for every +json type. A GET with an empty JSON error body does the same.
On 6c54c5b, against a local server:
http head $url # 404, content-type: application/json; charset=utf-8
# main: label "Not Found"
# branch: label ""
http options $url # 405, application/json; charset=utf-8, empty body
# main: label "Method Not Allowed"
# branch: label ""
http get $url # 401, application/json; charset=utf-8, empty body
# main: label "Unauthorized"
# branch: label ""The heading still says HTTP Error 404 (Not Found). Only the label under the URL goes blank.
Falling back to the reason phrase when the body is empty or can't be read fixes all three. I built this one and the 154 commands::network::http tests pass:
| response | |
| .body_mut() | |
| .read_to_string() | |
| .unwrap_or("Cannot read body".to_string()) | |
| .unwrap_or_else(|_| "Cannot read body".to_string()) | |
| response | |
| .body_mut() | |
| .read_to_string() | |
| .ok() | |
| .filter(|body| !body.trim().is_empty()) | |
| .unwrap_or_else(|| { | |
| response | |
| .status() | |
| .canonical_reason() | |
| .unwrap_or("") | |
| .to_string() | |
| }) |
An http head case with application/json; charset=utf-8 in the tests would catch it.
There was a problem hiding this comment.
Ok, for the empty body, but I still want to distinguish empty body and body read failure. I've just pushed a commit that does that.
a107e10 to
a3487b6
Compare
|
Thanks |
|
My pleasure! |



Description
This is a follow up to #18387, which provided the full body on failed http call for json responses.
The content-type check was naive and would not handle the case of the header having a charset, like in "application/json; charset=utf-8".
This PR fixes the feature to work for any json body.
User-facing changes (Release notes)
Additional notes
No related ticket, I went straight for the fix.