* `sniffio` - Async library autodetection.
* `rich` - Rich terminal support. *(Optional, with `httpx[cli]`)*
* `click` - Command line client support. *(Optional, with `httpx[cli]`)*
-* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
* `brotli` or `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional, with `httpx[brotli]`)*
+* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
A huge amount of credit is due to `requests` for the API layout that
much of this work follows, as well as to `urllib3` for plenty of design
* `sniffio` - Async library autodetection.
* `rich` - Rich terminal support. *(Optional, with `httpx[cli]`)*
* `click` - Command line client support. *(Optional, with `httpx[cli]`)*
-* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
* `brotli` or `brotlicffi` - Decoding for "brotli" compressed responses. *(Optional, with `httpx[brotli]`)*
+* `async_generator` - Backport support for `contextlib.asynccontextmanager`. *(Only required for Python 3.6)*
A huge amount of credit is due to `requests` for the API layout that
much of this work follows, as well as to `urllib3` for plenty of design
+import functools
import json
import sys
import typing
return "" # pragma: nocover
-def format_request_headers(request: Request) -> str:
+def format_request_headers(request: Request, http2: bool = False) -> str:
+ version = "HTTP/2" if http2 else "HTTP/1.1"
+ headers = [
+ (name.lower() if http2 else name, value) for name, value in request.headers.raw
+ ]
target = request.url.raw[-1].decode("ascii")
- lines = [f"{request.method} {target} HTTP/1.1"] + [
- f"{name.decode('ascii')}: {value.decode('ascii')}"
- for name, value in request.headers.raw
+ lines = [f"{request.method} {target} {version}"] + [
+ f"{name.decode('ascii')}: {value.decode('ascii')}" for name, value in headers
]
return "\n".join(lines)
return "\n".join(lines)
-def print_request_headers(request: Request) -> None:
+def print_request_headers(request: Request, http2: bool = False) -> None:
console = rich.console.Console()
- http_text = format_request_headers(request)
+ http_text = format_request_headers(request, http2=http2)
syntax = rich.syntax.Syntax(http_text, "http", theme="ansi_dark", word_wrap=True)
console.print(syntax)
syntax = rich.syntax.Syntax("", "http", theme="ansi_dark", word_wrap=True)
event_hooks: typing.Dict[str, typing.List[typing.Callable]] = {}
if verbose:
- event_hooks["request"] = [print_request_headers]
+ event_hooks["request"] = [functools.partial(print_request_headers, http2=http2)]
if follow_redirects:
event_hooks["response"] = [print_redirects]