Encode Multipart Post data at Request creation
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This fixes #298 (File upload not working with HTTP digest authentication)
authored by Pierre Chapuis · committed by Pierre Chapuis · parent c3e6c41 · Browse files · 1 file·+27 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+27 added−22 removed
- other:+27 added−22 removed
Provenance
- Commit
f3ad56f- Tree
0594174- Author
Pierre Chapuis <catwell@archlinux.us>- Committer
Pierre Chapuis <catwell@archlinux.us>- Parent
c3e6c41
1 file changed+27 added−22 removed
modified:requests/models.py+27 added-22 removed
78 unmodified lines79 #: Dictionary of HTTP Headers to attach to the :class:`Request <Request>`.80 self.headers = dict(headers or [])81 82 #: Dictionary of files to multipart upload (``{filename: content}``).83- self.files = files83+ self.files = None84 85 #: HTTP Method to use.86 self.method = method87 25 unmodified lines113 self.proxies['https'] = os.environ['HTTPS_PROXY']114 115 self.data, self._enc_data = self._encode_params(data)116 self.params, self._enc_params = self._encode_params(params)117+ self.files, self._enc_files = self._encode_files(files)118 119 #: :class:`Response <Response>` instance, containing120 #: content and metadata of HTTP Response, once :attr:`sent <send>`.121 self.response = Response()207 unmodified lines329 return result, urlencode(result, doseq=True)330 else:331 return data, data332 333+ def _encode_files(self,files):334+335+ if (not files) or isinstance(self.data, str):336+ return None, None337+338+ try:339+ fields = self.data.copy()340+ except AttributeError:341+ fields = dict(self.data)342+343+ for (k, v) in list(files.items()):344+ # support for explicit filename345+ if isinstance(v, (tuple, list)):346+ fn, fp = v347+ else:348+ fn = guess_filename(v) or k349+ fp = v350+ fields.update({k: (fn, fp.read())})351+352+ (body, content_type) = encode_multipart_formdata(fields)353+354+ return files, (body, content_type)355+356 @property357 def full_url(self):358 """Build the actual URL to use."""359 110 unmodified lines470 content_type = None471 472 # Multi-part file uploads.473 if self.files:450- if not isinstance(self.data, str):451-452- try:453- fields = self.data.copy()454- except AttributeError:455- fields = dict(self.data)456-457- for (k, v) in list(self.files.items()):458- # support for explicit filename459- if isinstance(v, (tuple, list)):460- fn, fp = v461- else:462- fn = guess_filename(v) or k463- fp = v464- fields.update({k: (fn, fp.read())})465-466- (body, content_type) = encode_multipart_formdata(fields)467- else:468- pass469- # TODO: Conflict?474+ (body, content_type) = self._enc_files475 else:476 if self.data:477 478 body = self._enc_data288 unmodified lines767 768 except AttributeError:769 self._content = None770 766- self._content_consumed = True 771+ self._content_consumed = True772 return self._content773 774 def _detected_encoding(self):775 try:59 unmodified lines