Skip to content

Conversation

@lovelydinosaur
Copy link
Contributor

Closes #1226

Creating a Request instance directly has neater more minimal header behaviour.
Only includes mandatory headers.

request = httpx.Request("POST", "http://example.org", json={"test": 123})

assert request.headers == httpx.Headers({
    "Host": "example.org",
    "Content-Type": "application/json",
    "Content-Length": "13",
})

Instead the default headers are on the client instance...

>>> client = httpx.Client()
>>> print(client.headers)
Headers({'accept': '*/*', 'accept-encoding': 'gzip, deflate, br', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.14.2'})

Can include additional headers...

>>> client = httpx.Client(headers={'Custom-Header': 'Custom'})
>>> print(client.headers)
Headers({'accept': '*/*', 'accept-encoding': 'gzip, deflate, br', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.14.2', 'custom-header': 'Custom'})

Or override existing headers...

>>> client = httpx.Client(headers={'User-Agent': 'Custom'})
>>> print(client.headers)
Headers({'accept': '*/*', 'accept-encoding': 'gzip, deflate, br', 'connection': 'keep-alive', 'user-agent': 'Custom'})

Defaults may be removed, but must be done as an explicit del...

>>> client = httpx.Client()
>>> del client.headers['User-Agent']
>>> print(client.headers)
Headers({'accept': '*/*', 'accept-encoding': 'gzip, deflate, br', 'connection': 'keep-alive'})

@lovelydinosaur lovelydinosaur added the enhancement New feature or request label Sep 2, 2020
Copy link
Contributor

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

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

Very nice and sweet indeed!

@lovelydinosaur lovelydinosaur merged commit 19b863a into master Sep 2, 2020
@lovelydinosaur lovelydinosaur deleted the header-refinements branch September 2, 2020 20:32
@lovelydinosaur lovelydinosaur mentioned this pull request Sep 21, 2020
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refine headers behaviour for Request() and client.build_request()

2 participants