#: HTTP Method to use.
self.method = method
- #: Dictionary or byte of request body data to attach to the
+ #: Dictionary, bytes or file stream of request body data to attach to the
#: :class:`Request <Request>`.
self.data = None
return data
if isinstance(data, str):
return data
+ elif hasattr(data, 'read'):
+ return data
elif hasattr(data, '__iter__'):
try:
dict(data)
if self.data:
body = self._encode_params(self.data)
- if isinstance(self.data, str):
+ if isinstance(self.data, str) or hasattr(self.data, 'read'):
content_type = None
else:
content_type = 'application/x-www-form-urlencoded'
import os
import unittest
import pickle
+import tempfile
import requests
from requests.compat import str, StringIO
assert rbody.get('form') in (None, {})
self.assertEqual(rbody.get('data'), 'fooaowpeuf')
+ def test_file_post_data(self):
+
+ filecontent = "fooaowpeufbarasjhf"
+ testfile = tempfile.NamedTemporaryFile()
+ testfile.write(filecontent)
+ testfile.flush()
+
+ for service in SERVICES:
+
+ r = post(service('post'), data=open(testfile.name, "rb"),
+ headers={"content-type": "application/octet-stream"})
+
+ self.assertEqual(r.status_code, 200)
+ self.assertEqual(r.headers['content-type'], 'application/json')
+ self.assertEqual(r.url, service('post'))
+
+ rbody = json.loads(r.text)
+ assert rbody.get('form') in (None, {})
+ self.assertEqual(rbody.get('data'), filecontent)
+
def test_urlencoded_post_querystring(self):
for service in SERVICES: