Repository navigation
Improve headers ergonomics - #705
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Headers.update()accept strings or bytes, so
response.headers["X-Example"] = "value"issupported while headers remain stored internally as bytes. String names use
ASCII and string values use Latin-1; unrepresentable characters raise
UnicodeEncodeError. Assignment still replaces all case-insensitive matches.Headers.get_text_values(name, encoding="latin-1")to decode matchingvalues on demand as a tuple, preserving repeated header entries.
get_first_text()returns the first value as a string, orNonewhen absent;get_single_text()returns a string and requires exactly one matching entry,raising
ValueErrorotherwise. Both also accept an explicit encoding.Existing byte-valued reads and low-level byte-based methods remain unchanged.
string or byte names; lookup always returns byte values. Add
Headers.add_text()to append without replacing matches, and
Headers.from_values()to build abyte-backed collection from mappings or iterables while preserving duplicates.
headers=anddefault_headers=, route header filters, and testing-client iterable headers.Previously, HTTP client string values required ASCII. Testing-client dictionaries
retain their historical UTF-8 encoding for backward compatibility.
Pass explicitly encoded bytes for field-specific encodings.
as UTF-8, so Latin-1 values and explicitly encoded bytes reach the wire unchanged.