Skip to content

Commit

Permalink
Merge pull request #559 from barberj/issue_526
Browse files Browse the repository at this point in the history
Issue #526
  • Loading branch information
Kenneth Reitz committed May 8, 2012
2 parents 7b6338c + b066902 commit e627c1f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -98,3 +98,4 @@ Patches and Suggestions
- Shivaram Lingamneni
- Miguel Turner
- Rohan Jain (crodjer)
- Justin Barber <barber.justin@gmail.com>
28 changes: 28 additions & 0 deletions docs/user/quickstart.rst
Expand Up @@ -222,6 +222,34 @@ Setting filename explicitly::
"data": ""
}

Sending strings to be received as files::

>>> url = 'http://httpbin.org/post'
>>> files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}

>>> r = requests.post(url, files=files)
>>> r.text
{
"origin": "179.13.100.4",
"files": {
"file": "some,data,to,send\\nanother,row,to,send\\n"
},
"form": {},
"url": "http://httpbin.org/post",
"args": {},
"headers": {
"Content-Length": "216",
"Accept-Encoding": "identity, deflate, compress, gzip",
"Connection": "keep-alive",
"Accept": "*/*",
"User-Agent": "python-requests/0.11.1",
"Host": "httpbin.org",
"Content-Type": "multipart/form-data; boundary=127.0.0.1.502.41433.1335385481.788.1"
},
"json": null,
"data": ""
}


Response Status Codes
---------------------
Expand Down
4 changes: 3 additions & 1 deletion requests/models.py
Expand Up @@ -32,7 +32,7 @@
DEFAULT_CA_BUNDLE_PATH)
from .compat import (
cookielib, urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes,
is_py2)
StringIO, is_py2)

# Import chardet if it is available.
try:
Expand Down Expand Up @@ -348,6 +348,8 @@ def _encode_files(self, files):
else:
fn = guess_filename(v) or k
fp = v
if isinstance(fp, (bytes, str)):
fp = StringIO(fp)
fields.update({k: (fn, fp.read())})

(body, content_type) = encode_multipart_formdata(fields)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_requests.py
Expand Up @@ -329,6 +329,37 @@ def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self):

self.assertEqual(post2.status_code, 200)

def test_POSTBIN_GET_POST_FILES_STRINGS(self):

for service in SERVICES:

url = service('post')

post1 = post(url, files={'fname.txt': 'fdata'})
self.assertEqual(post1.status_code, 200)

post2 = post(url, files={'fname.txt': 'fdata', 'fname2.txt':'more fdata'})
self.assertEqual(post2.status_code, 200)

post3 = post(url, files={'fname.txt': 'fdata', 'fname2.txt':open(__file__,'rb')})
self.assertEqual(post3.status_code, 200)

post4 = post(url, files={'fname.txt': 'fdata'})
self.assertEqual(post4.status_code, 200)

post5 = post(url, files={'file': ('file.txt', 'more fdata')})
self.assertEqual(post5.status_code, 200)

post6 = post(url, files={'fname.txt': '\xe9'})
self.assertEqual(post6.status_code, 200)

post7 = post(url, files={'fname.txt': 'fdata to verify'})
rbody = json.loads(post7.text)
self.assertTrue(rbody.get('files', None))
self.assertTrue(rbody['files'].get('fname.txt'), None)
self.assertEqual(rbody['files']['fname.txt'], 'fdata to verify')


def test_nonzero_evaluation(self):

for service in SERVICES:
Expand Down

0 comments on commit e627c1f

Please sign in to comment.