Skip to content

Fix http response body error message for some content-type - #19121

Merged
fdncred merged 3 commits into
nushell:mainfrom
madjar:http-content-type
Nov 16, 2026
Merged

fdncred merged 3 commits into
nushell:mainfrom
madjar:http-content-type

Conversation

@madjar

@madjar madjar commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

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)

  • Fixed an issue where some http errors with json response bodies would not be displayed to the user.

Additional notes

No related ticket, I went straight for the fix.

@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Sep 30, 2026
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"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. I went for matching exactly two cases: charset=utf-8, and no charset specified.

@fdncred

fdncred commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

please try and get the ci green

@madjar
madjar force-pushed the http-content-type branch from ef66131 to 95ee8d6 Compare October 1, 2026 10:38
@madjar

madjar commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Oh, sorry. Fixed.

@fdncred
fdncred requested a balanced review from Copilot October 9, 2026 12:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@fdncred fdncred left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@fdncred

fdncred commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

where are we at on this?

@madjar

madjar commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

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 transform_response_using_content_type as it probably deserves its own PR) and adds a test case.

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

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity · 1 Medium severity · 0 Low severity

Open (1)

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.

Comment on lines +994 to +997
response
.body_mut()
.read_to_string()
.unwrap_or("Cannot read body".to_string())
.unwrap_or_else(|_| "Cannot read body".to_string())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium severity Empty JSON error bodies now blank the error label

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:

Suggested change
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@fdncred
fdncred merged commit 876912d into nushell:main Nov 16, 2026
14 checks passed
@fdncred

fdncred commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 16, 2026
@madjar

madjar commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

My pleasure!

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

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants