Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header refinements #1248

Merged
merged 2 commits into from
Sep 2, 2020
Merged

Header refinements #1248

merged 2 commits into from
Sep 2, 2020

Conversation

tomchristie
Copy link
Member

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'})

@tomchristie tomchristie added the enhancement New feature or request label Sep 2, 2020
Copy link
Member

@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!

@tomchristie tomchristie merged commit 19b863a into master Sep 2, 2020
@tomchristie tomchristie deleted the header-refinements branch September 2, 2020 20:32
@tomchristie tomchristie 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