From 75d18a5b3f23fa135ffc85fc3565ed4f31ae97d3 Mon Sep 17 00:00:00 2001 From: meat-daemon-404 <3335264558@qq.com> Date: Fri, 27 Nov 2026 19:51:12 +0800 Subject: [PATCH] Warn about --ignore-stdin when a request with implicit stdin fails Emit a warning suggesting --ignore-stdin when an HTTP request that implicitly read redirected stdin as its body fails with a 4xx/5xx response. Fixes #1525 --- httpie/core.py | 16 ++++++++ tests/test_ignore_stdin_warning.py | 62 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 tests/test_ignore_stdin_warning.py diff --git a/httpie/core.py b/httpie/core.py index d0c26dcbcc..d308a1d18a 100644 --- a/httpie/core.py +++ b/httpie/core.py @@ -241,6 +241,22 @@ def request_body_read_callback(chunk: bytes): ) prev_with_body = output_options.body + if ( + final_response is not None + and final_response.status_code >= 400 + and env.stdin + and not env.stdin_isatty + and not args.ignore_stdin + and args.raw is None + and initial_request is not None + and initial_request.body + ): + env.log_error( + 'stdin contents detected on a failed request. ' + 'Retry with --ignore-stdin if stdin was not intended as request body.', + level=LogLevel.WARNING, + ) + # Cleanup if force_separator: separate() diff --git a/tests/test_ignore_stdin_warning.py b/tests/test_ignore_stdin_warning.py new file mode 100644 index 0000000000..d2eeb6482c --- /dev/null +++ b/tests/test_ignore_stdin_warning.py @@ -0,0 +1,62 @@ +"""Regression tests for --ignore-stdin warning on failed requests with stdin (#1525).""" + +import io +from unittest.mock import patch + +import requests + +from httpie.cli.definition import parser +from httpie.context import Environment +from httpie.core import program + + +def _make_pair(status_code: int, body=b'leaked-stdin'): + req = requests.Request('POST', 'http://example.com/api', data=body).prepare() + resp = requests.Response() + resp.status_code = status_code + resp.reason = 'Bad Request' if status_code == 400 else 'OK' + payload = b'Bad Request' if status_code == 400 else b'OK' + resp.raw = io.BytesIO(payload) + resp.raw.version = 11 + resp.raw.status = status_code + resp.raw.reason = resp.reason + resp.request = req + resp.url = req.url + return [req, resp] + + +def _run_program(cli_args, status_code, stdin_isatty=False, stdin_bytes=b'leaked-stdin'): + stdout = io.BytesIO() + stderr = io.StringIO() + stdin = io.BytesIO(stdin_bytes) + env = Environment( + stdin=stdin, + stdin_isatty=stdin_isatty, + stdout=stdout, + stdout_isatty=False, + stderr=stderr, + stderr_isatty=False, + ) + args = parser.parse_args(args=cli_args, env=env) + messages = _make_pair( + status_code, + body=stdin_bytes if not args.ignore_stdin and not stdin_isatty else None, + ) + with patch('httpie.core.collect_messages', return_value=messages): + program(args=args, env=env) + return stderr.getvalue() + + +def test_warns_about_ignore_stdin_on_failed_request_with_stdin(): + err = _run_program(['http://example.com/api'], status_code=400, stdin_isatty=False) + assert '--ignore-stdin' in err + + +def test_no_warning_when_ignore_stdin_is_passed(): + err = _run_program(['--ignore-stdin', 'http://example.com/api'], status_code=400, stdin_isatty=False) + assert '--ignore-stdin' not in err + + +def test_no_warning_on_successful_request_with_stdin(): + err = _run_program(['http://example.com/api'], status_code=200, stdin_isatty=False) + assert '--ignore-stdin' not in err