httpie / cli

🥧 HTTPie CLI — modern, user-friendly command-line HTTP client for the API era. JSON support, colors, sessions, downloads, plugins & more.
https://httpie.io
BSD 3-Clause "New" or "Revised" License
33.56k stars 3.67k forks source link

backslashes that precede a number are not preserved #1408

Open ducaale opened 2 years ago

ducaale commented 2 years ago

Checklist


Minimal reproduction code and steps

$ http --offline --print=B : \\0=

Current result

{
    "0": ""
}

Expected result

{
    "\\0": ""
}

Debug output

Please re-run the command with --debug, then copy the entire command & output and paste both below:

$ http --debug --offline --print=B : \\0=
HTTPie 3.2.1
Requests 2.22.0
Pygments 2.7.2
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0]
/usr/bin/python3
Linux 5.10.102.1-microsoft-standard-WSL2

<Environment {'apply_warnings_filter': <function Environment.apply_warnings_filter at 0x7f494214a5e0>,
 'args': Namespace(),
 'as_silent': <function Environment.as_silent at 0x7f494214a4c0>,
 'colors': 256,
 'config': {'default_options': []},
 'config_dir': PosixPath('/home/ducaale/.httpie'),
 'devnull': <property object at 0x7f494212a310>,
 'is_windows': False,
 'log_error': <function Environment.log_error at 0x7f494214a550>,
 'program_name': 'http',
 'quiet': 0,
 'rich_console': <functools.cached_property object at 0x7f49421c37f0>,
 'rich_error_console': <functools.cached_property object at 0x7f4942122910>,
 'show_displays': True,
 'stderr': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>,
 'stderr_isatty': True,
 'stdin': <_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>,
 'stdin_encoding': 'utf-8',
 'stdin_isatty': True,
 'stdout': <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>,
 'stdout_encoding': 'utf-8',
 'stdout_isatty': True}>

<PluginManager {'adapters': [],
 'auth': [<class 'httpie.plugins.builtin.BasicAuthPlugin'>,
          <class 'httpie.plugins.builtin.DigestAuthPlugin'>,
          <class 'httpie.plugins.builtin.BearerAuthPlugin'>],
 'converters': [],
 'formatters': [<class 'httpie.output.formatters.headers.HeadersFormatter'>,
                <class 'httpie.output.formatters.json.JSONFormatter'>,
                <class 'httpie.output.formatters.xml.XMLFormatter'>,
                <class 'httpie.output.formatters.colors.ColorFormatter'>]}>

>>> requests.request(**{'auth': None,
 'data': b'{"0": ""}',
 'headers': <HTTPHeadersDict('User-Agent': b'HTTPie/3.2.1', 'Accept': b'application/json, */*;q=0.5', 'Content-Type': b'application/json')>,
 'method': 'post',
 'params': <generator object MultiValueOrderedDict.items at 0x7f4941d267b0>,
 'url': 'http://localhost'})

{
    "0": ""
}

Additional information, screenshots, or code examples

N/A

YSCohen commented 2 years ago

Probably because of your shell. It works fine for me on PowerShell

> http --offline --print=B : \\0=
{
    "\\0": ""
}
ducaale commented 2 years ago

@YSCohen can you confirm which version of HTTPie you are using? I think the issue I am reporting is something that was introduced by the nested json changes.

YSCohen commented 2 years ago

3.2.1

YSCohen commented 2 years ago

If I do it in WSL it drops the backslashes.

$ python3 -m httpie --offline --print=B : \\0=
{
    "0": ""
}
blyxxyz commented 2 years ago

Probably because of your shell. It works fine for me on PowerShell

> http --offline --print=B : \\0=
{
    "\\0": ""
}

PowerShell doesn't treat backslashes specially so you're actually handing HTTPie two backslashes instead of one. It's clearer if we use single quotes. Bash:

$ http --offline --print=B : '\0='
{
    "0": ""
}

$ http --offline --print=B : '\\0='
{
    "\\0": ""
}

PowerShell:

PS > http.exe --offline --print=B : '\0='
{
    "0": ""
}

PS > http.exe --offline --print=B : '\\0='
{
    "\\0": ""
}

Not mentioned in the OP is that this used to work differently:

$ git checkout 2.0.0
HEAD is now at a7e5228 Cleanup
$ python3 -m httpie --offline --print=B : '\0='
{
    "\\0": ""
}
jkbrzt commented 2 years ago

Yes, it’s a bug. It looks like the nested JSON array notation is getting in the way. Here’s a minimal reproduction:

$ http --offline pie.dev/post '\0=\0'
{
    "0": "\\0"
}