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
Allow custom headers in multipart/form-data requests #1936
Merged
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b6a4c5b
feat: allow passing multipart headers
adriangb 01fc2fc
Add test for including content-type in headers
adriangb 79fcd72
lint
adriangb 08f96bf
Merge branch 'master' into multipart-advanced
adriangb 9421685
Merge branch 'master' into multipart-advanced
adriangb a43ffe7
Merge branch 'master' into multipart-advanced
adriangb cbb9f05
Merge branch 'master' into multipart-advanced
adriangb 0a913b9
Merge branch 'master' into multipart-advanced
adriangb c55188d
Merge branch 'master' into multipart-advanced
adriangb 8e04a40
Merge branch 'master' into multipart-advanced
adriangb 6fe8c06
Merge branch 'master' into multipart-advanced
adriangb ec474fd
override content_type with headers
adriangb 79d1521
compare tuples based on length
adriangb 195c661
incorporate suggestion
adriangb 1c9dc26
remove .title() on headers
adriangb File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,23 +78,41 @@ def __init__(self, name: str, value: FileTypes) -> None: | |
|
||
fileobj: FileContent | ||
|
||
headers: typing.Dict[str, str] = {} | ||
content_type: typing.Optional[str] = None | ||
|
||
# This large tuple based API largely mirror's requests' API | ||
# It would be good to think of better APIs for this that we could include in httpx 2.0 | ||
# since variable length tuples (especially of 4 elements) are quite unwieldly | ||
if isinstance(value, tuple): | ||
try: | ||
filename, fileobj, content_type = value # type: ignore | ||
except ValueError: | ||
if len(value) == 2: | ||
# neither the 3rd parameter (content_type) nor the 4th (headers) was included | ||
filename, fileobj = value # type: ignore | ||
content_type = guess_content_type(filename) | ||
elif len(value) == 3: | ||
filename, fileobj, content_type = value # type: ignore | ||
else: | ||
# all 4 parameters included | ||
filename, fileobj, content_type, headers = value # type: ignore | ||
headers = {k.title(): v for k, v in headers.items()} | ||
else: | ||
filename = Path(str(getattr(value, "name", "upload"))).name | ||
fileobj = value | ||
|
||
if content_type is None: | ||
content_type = guess_content_type(filename) | ||
|
||
if content_type is not None and "Content-Type" not in headers: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps... has_content_type_header = any(["content-type" in key.lower() for key in headers])
if content_type is not None and not has_content_type_header:
... ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I adapted it to |
||
# note that unlike requests, we ignore the content_type | ||
# provided in the 3rd tuple element if it is also included in the headers | ||
# requests does the opposite (it overwrites the header with the 3rd tuple element) | ||
headers["Content-Type"] = content_type | ||
|
||
if isinstance(fileobj, (str, io.StringIO)): | ||
raise TypeError(f"Expected bytes or bytes-like object got: {type(fileobj)}") | ||
|
||
self.filename = filename | ||
self.file = fileobj | ||
self.content_type = content_type | ||
self.headers = headers | ||
self._consumed = False | ||
|
||
def get_length(self) -> int: | ||
|
@@ -122,9 +140,9 @@ def render_headers(self) -> bytes: | |
if self.filename: | ||
filename = format_form_param("filename", self.filename) | ||
parts.extend([b"; ", filename]) | ||
if self.content_type is not None: | ||
content_type = self.content_type.encode() | ||
parts.extend([b"\r\nContent-Type: ", content_type]) | ||
for header_name, header_value in self.headers.items(): | ||
key, val = f"\r\n{header_name}: ".encode(), header_value.encode() | ||
parts.extend([key, val]) | ||
parts.append(b"\r\n\r\n") | ||
self._headers = b"".join(parts) | ||
|
||
|
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should
.title()
case here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah... I see the comparison case. Huh. Fiddly.