def transparent_encode_multipart(func):
"""Wrap an HTTP client method, transparently encoding multipart data.
This wraps some of Django's `Client` HTTP verb methods -- delete, options,
patch, put -- so they accept a dict of data to be sent as part of the
request body, in MIME multipart encoding.
This also accepts an optional dict of query parameters (as `query`) to be
encoded as a query string and appended to the given path.
Since Django 1.5, these HTTP verb methods require data in the form of a
byte string. The application (that's us) needs to take care of MIME
encoding.
"""
@wraps(func)
def maybe_encode_multipart(
self, path, data=b"", content_type=None, secure=False, query=None,
**extra):
if isinstance(data, bytes):
if content_type is None:
content_type = 'application/octet-stream'
elif content_type is None:
content_type = client.MULTIPART_CONTENT
data = client.encode_multipart(client.BOUNDARY, data)
else:
raise TypeError(
"Cannot combine data (%r) with content-type (%r)."
% (data, content_type))
if query is not None:
query = urlencode(query, doseq=True)
path = path + ("&" if "?" in path else "?") + query
return func(self, path, data, content_type, secure, **extra)
return maybe_encode_multipart
评论列表
文章目录