-
Notifications
You must be signed in to change notification settings - Fork 140
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 overriding of the default Content-Type in POST and PUT #301
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
treq request functions and methods like :func:`treq.put()` and :meth:`HTTPClient.post()` now accept Content-Type overrides through the headers kwarg | ||
Content-Types will still default to the previous default values depending on the `data`, `files`, and `json` parameters. |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -39,6 +39,7 @@ | |||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
_NOTHING = object() | ||||||||||||||||||||||||||||||||
CONTENT_TYPE_HEADER_NAME = b'Content-Type' | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def urlencode(query, doseq): | ||||||||||||||||||||||||||||||||
|
@@ -192,9 +193,13 @@ def request(self, method, url, **kwargs): | |||||||||||||||||||||||||||||||
files=kwargs.pop('files', None), | ||||||||||||||||||||||||||||||||
json=kwargs.pop('json', _NOTHING), | ||||||||||||||||||||||||||||||||
stacklevel=stacklevel, | ||||||||||||||||||||||||||||||||
headers=headers, | ||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||
if contentType is not None: | ||||||||||||||||||||||||||||||||
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. Since headers is now passed into |
||||||||||||||||||||||||||||||||
headers.setRawHeaders(b'Content-Type', [contentType]) | ||||||||||||||||||||||||||||||||
headers.setRawHeaders( | ||||||||||||||||||||||||||||||||
CONTENT_TYPE_HEADER_NAME, | ||||||||||||||||||||||||||||||||
[contentType] | ||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
cookies = kwargs.pop('cookies', {}) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
@@ -252,7 +257,7 @@ def gotResult(result): | |||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return d.addCallback(_Response, cookies) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def _request_body(self, data, files, json, stacklevel): | ||||||||||||||||||||||||||||||||
def _request_body(self, data, files, json, stacklevel, headers): | ||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||
Here we choose a right producer based on the parameters passed in. | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
@@ -301,17 +306,25 @@ def _request_body(self, data, files, json, stacklevel): | |||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||
data = [] | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
content_type = _get_content_type_from_header( | ||||||||||||||||||||||||||||||||
headers, | ||||||||||||||||||||||||||||||||
b'multipart/form-data; boundary=' + boundary | ||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||
multipart.MultiPartProducer(data + files, boundary=boundary), | ||||||||||||||||||||||||||||||||
b'multipart/form-data; boundary=' + boundary, | ||||||||||||||||||||||||||||||||
content_type, | ||||||||||||||||||||||||||||||||
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. Won't setting the content type in this case make the request body unparseable for lack of the |
||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
# Otherwise stick to x-www-form-urlencoded format | ||||||||||||||||||||||||||||||||
# as it's generally faster for smaller requests. | ||||||||||||||||||||||||||||||||
if isinstance(data, (dict, list, tuple)): | ||||||||||||||||||||||||||||||||
content_type = _get_content_type_from_header( | ||||||||||||||||||||||||||||||||
headers, | ||||||||||||||||||||||||||||||||
b'application/x-www-form-urlencoded' | ||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||
self._data_to_body_producer(urlencode(data, doseq=True)), | ||||||||||||||||||||||||||||||||
b'application/x-www-form-urlencoded', | ||||||||||||||||||||||||||||||||
content_type, | ||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||
elif data: | ||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||
|
@@ -320,11 +333,15 @@ def _request_body(self, data, files, json, stacklevel): | |||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if json is not _NOTHING: | ||||||||||||||||||||||||||||||||
content_type = _get_content_type_from_header( | ||||||||||||||||||||||||||||||||
headers, | ||||||||||||||||||||||||||||||||
b'application/json; charset=UTF-8' | ||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||
self._data_to_body_producer( | ||||||||||||||||||||||||||||||||
json_dumps(json, separators=(u',', u':')).encode('utf-8'), | ||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||
b'application/json; charset=UTF-8', | ||||||||||||||||||||||||||||||||
content_type, | ||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return None, None | ||||||||||||||||||||||||||||||||
|
@@ -431,6 +448,28 @@ def _guess_content_type(filename): | |||||||||||||||||||||||||||||||
return guessed or 'application/octet-stream' | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
def _get_content_type_from_header(headers, default=None): | ||||||||||||||||||||||||||||||||
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. Seems like this should be called |
||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||
Retrieve Content-Type header from twisted.web.http_headers.Headers object | ||||||||||||||||||||||||||||||||
or return supplied default | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
:param headers: | ||||||||||||||||||||||||||||||||
A twisted.web.http_headers.Headers object | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
:param default: | ||||||||||||||||||||||||||||||||
The default Content-Type to return, encoded as a byte-string | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
:returns: | ||||||||||||||||||||||||||||||||
byte-string containing a Content-Type value | ||||||||||||||||||||||||||||||||
:rtype: | ||||||||||||||||||||||||||||||||
bytes | ||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||
if not 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. At this point headers is always a Lines 174 to 188 in d45cad2
This clause is dead code and should be removed (at which point this function doesn't do much...) |
||||||||||||||||||||||||||||||||
return default | ||||||||||||||||||||||||||||||||
return headers.getRawHeaders( | ||||||||||||||||||||||||||||||||
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 think that this function should raise an exception when there are multiple content-type headers (since that's not valid). I think it's okay to break messily in this case, so you could unpack like: [content_type] = headers.getRawHeaders(CONTENT_TYPE_HEADER_NAME, [default])
return content_type |
||||||||||||||||||||||||||||||||
CONTENT_TYPE_HEADER_NAME, [default] | ||||||||||||||||||||||||||||||||
)[0] | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
registerAdapter(_from_bytes, bytes, IBodyProducer) | ||||||||||||||||||||||||||||||||
registerAdapter(_from_file, io.BytesIO, IBodyProducer) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
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.
This is a personal design decision to reduce typos. Please let me know if it runs counter to the project's intended design.
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.
Having a constant isn't my personal preference — tests should catch any typos, and in this case the constant name is longer than the actual value and no more readable — but I'm okay with it. However, the constant should definitely be a private to the module, so please rename it to
_CONTENT_TYPE_HEADER_NAME
.