From b252cd84037053bd7675d1ff41c102ce237cd0ae Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Fri, 25 Feb 2011 08:00:11 -0500 Subject: [PATCH] allow files to be upload along with form data --- requests/core.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/requests/core.py b/requests/core.py index 13dc5c4..b426a4e 100644 --- a/requests/core.py +++ b/requests/core.py @@ -68,12 +68,13 @@ class Request(object): self.headers = headers self.files = files self.method = method + self.data = data # url encode data if it's a dict if hasattr(data, 'items'): - self.data = urllib.urlencode(data) + self._enc_data = urllib.urlencode(data) else: - self.data = data + self._enc_data = data self.response = Response() @@ -161,18 +162,20 @@ class Request(object): success = False if self.method in ('GET', 'HEAD', 'DELETE'): - req = _Request(self._build_url(self.url, self.data), method=self.method) + req = _Request(self._build_url(self.url, self._enc_data), method=self.method) else: + if self.files: register_openers() -# self.files + + if self.data: + self.files.update(self.data) + datagen, headers = multipart_encode(self.files) req = _Request(self.url, data=datagen, headers=headers, method=self.method) + else: - req = _Request(self.url, method=self.method) - - if self.data: - req.data = self.data + req = _Request(self.url, data=self._enc_data, method=self.method) if self.headers: req.headers = self.headers -- 2.7.4