Skip to content

Commit e17546d

Browse files
Improve REQUEST_ITEM error for unquoted values with spaces
1 parent 5b604c3 commit e17546d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

httpie/cli/argtypes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ def __call__(self, s: str) -> KeyValueArg:
103103
break
104104

105105
else:
106+
# Check if this looks like it should be a continuation of a previous value
107+
# (i.e., it's a string without any separator, likely due to unquoted shell args)
108+
if all(c not in s for c in self.special_characters):
109+
raise argparse.ArgumentTypeError(
110+
f'{s!r} is not a valid value. Did you mean to quote it as part of a previous item? '
111+
f'For example: field="value {s}"'
112+
)
106113
raise argparse.ArgumentTypeError(f'{s!r} is not a valid value')
107114

108115
return self.key_value_class(key=key, value=value, sep=sep, orig=s)

tests/test_cli.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ def test_invalid_items(self):
2727
for item in items:
2828
pytest.raises(argparse.ArgumentTypeError, self.key_value_arg, item)
2929

30+
def test_invalid_item_without_separator_provides_helpful_error(self):
31+
"""Test that items without separators show a helpful error message."""
32+
with pytest.raises(argparse.ArgumentTypeError) as exc_info:
33+
self.key_value_arg('Bar')
34+
assert 'Did you mean to quote it' in str(exc_info.value)
35+
assert 'field="value Bar"' in str(exc_info.value)
36+
3037
def test_escape_separator(self):
3138
items = RequestItems.from_args([
3239
# headers
@@ -63,6 +70,16 @@ def test_backslash_before_non_special_character_does_not_escape(
6370
actual = self.key_value_arg(string)
6471
assert actual == expected
6572

73+
@pytest.mark.parametrize('item_string, expected_value', [
74+
('field=value with spaces', 'value with spaces'),
75+
('field_name="Foo Bar"', '"Foo Bar"'),
76+
('data=Hello World Test', 'Hello World Test'),
77+
])
78+
def test_values_with_spaces_when_properly_quoted(self, item_string, expected_value):
79+
"""Test that values with spaces work correctly when properly quoted."""
80+
result = self.key_value_arg(item_string)
81+
assert result.value == expected_value
82+
6683
def test_escape_longsep(self):
6784
items = RequestItems.from_args([
6885
self.key_value_arg(r'bob\:==foo'),

0 commit comments

Comments
 (0)