Merge branch 'feature/curl' into develop
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Conflicts: AUTHORS HISTORY.rst
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 1a64a29, b6aa868 · Browse files · 5 files·+123 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+123 added−1 removed
- other:+123 added−1 removed
Provenance
- Commit
dd5a397- Tree
4f61797- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
1a64a29- Parent 2
b6aa868
5 files changed+123 added−1 removed
modified:docs/api.rst+4 added
46 unmodified lines47 .. autofunction:: dict_from_cookiejar48 .. autofunction:: cookiejar_from_dict49 .. autofunction:: add_dict_to_cookiejar50 51+Curl52+~~~~53+.. autofunction:: curl_from_request54+55 Encodings56 ~~~~~~~~~57 58 .. autofunction:: get_encodings_from_content35 unmodified linesmodified:requests/utils.py+70 added-1 removed
214 unmodified lines215 216 :param content: bytestring to gzip-decode.217 """218 219- return zlib.decompress(content, 16+zlib.MAX_WBITS)219+ return zlib.decompress(content, 16+zlib.MAX_WBITS)220+221+222+def curl_from_request(request):223+ """Returns a curl command from the request.224+225+ :param request: The :class:`Request <Request>` object226+227+ Example:228+ | import requests229+ | from requests.utils import curl_from_request230+ | r = requests.get('http://httpbin.org/get')231+ | curl_from_request(r.request)232+233+ """234+235+ #TODO - OAuth236+237+ #: -L/--location - if there is a redirect, redo request on the new place.238+ curl = 'curl -L '239+240+ #: -u/--user - Specify the user name and password to use for server auth.241+ #: Basic Auth only for now242+ auth = ''243+ if request.auth is not None:244+ auth = '-u "%s:%s" ' % (request.auth.username, request.auth.password)245+246+ method = ''247+ if request.method.upper() == 'HEAD':248+ #: -I/--head - fetch headers only.249+ method = '-I '250+ else:251+ #: -X/--request - specify request method.252+ method = '-X %s ' % request.method.upper()253+254+ #: -b/--cookie255+ #: (HTTP) Pass the data to the HTTP server as a cookie. It is supposedly the256+ #: data previously received from the server in a "Set-Cookie:" line.257+ cookies = ''258+ if request.cookiejar:259+ cookies = cookies.join(['-b "%s=%s" ' % (k.name, k.value) for k in request.cookiejar])260+261+ #: -H/--header - Extra header to use when getting a web page.262+ header = ''263+ if request.headers:264+ header = header.join(['-H "%s:%s" ' % (k, v) for k, v in request.headers.iteritems()])265+266+ form = ''267+ if request.method in ('PUT', 'POST', 'PATCH'):268+ #: request.files is updated with request.data if both exist, so only iterate request.files.269+ #: ContentType multipart/form-data is used.270+ if request.files:271+ #: -F/--form - Emulate form data. To force 'content' to a file, prefix file name @.272+ for k, v in request.files.iteritems():273+ if isinstance(v, file):274+ form = form + '-F "%s=@%s" ' % (k, v.name)275+ elif v not in (None, ''):276+ form = form + '-F "%s=%s" ' % (k, v)277+278+ #: content-type application/x-www-form-urlencoded is used here.279+ else:280+ #: -d/--data - send specified data in post request.281+ if isinstance(request.data, (list, tuple)):282+ form = form.join(['-d "%s=%s" ' % (k, v) for k, v in request.data])283+ elif request._enc_data not in (None, ''):284+ form = "-d '%s' " % (request._enc_data)285+286+ #: Params handled in _build_url287+ return curl + auth + method + header + cookies + form + '"' + request._build_url() + '"'288+modified:AUTHORS+1 added
41 unmodified lines42 - Alejandro Giacometti43 - Rick Mak44 - Johan Bergström45 - Josselin Jacquard46+- Mike Waldnermodified:HISTORY.rst+1 added
4 unmodified lines5 * New ``decode_unicode`` setting6 * Removal of ``r.read/close`` methods7 * New ``r.fo`` interface for advanced response usage.*8 * Automatic expansion of parameterized headers9+* Turn off Redirects for GET/HEAD requests10 11 0.6.1 (2011-08-20)12 ++++++++++++++++++13 154 unmodified linesmodified:test_requests.py+47 added
12 unmodified lines13 14 import requests15 16 from requests.sessions import Session17+from requests.utils import curl_from_request18 19 20 HTTPBIN_URL = 'http://httpbin.org/'21 HTTPSBIN_URL = 'https://httpbin.ep.io/'455 unmodified lines477 assert heads['User-agent'] in r2.content478 self.assertEqual(r2.status_code, 200)479 480 481+ def test_curl_HTTP_OK_GET(self):482+ curl_str = 'curl -L -X GET -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" "http://httpbin.org//"'483+ r = requests.get(httpbin('/'))484+ self.assertEqual(curl_from_request(r.request), curl_str)485 486+487+ def test_curl_HTTP_OK_GET_WITH_PARAMS(self):488+ curl_str = 'curl -L -X GET -H "Accept-Encoding:gzip" -H "User-agent:Mozilla/5.0" "http://httpbin.org/user-agent"'489+490+ heads = {'User-agent': 'Mozilla/5.0'}491+ r = requests.get(httpbin('user-agent'), headers=heads)492+ self.assertEqual(curl_from_request(r.request), curl_str)493+494+495+ def test_curl_HTTP_OK_HEAD(self):496+ curl_str ='curl -L -I -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" "http://httpbin.org//"'497+ r = requests.head(httpbin('/'))498+ self.assertEqual(curl_from_request(r.request), curl_str)499+500+501+ def test_curl_HTTP_OK_PATCH(self):502+ curl_str = 'curl -L -X PATCH -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" "http://httpbin.org/patch"'503+ r = requests.patch(httpbin('patch'))504+ self.assertEqual(curl_from_request(r.request), curl_str)505+506+507+ def test_curl_AUTH_HTTPS_OK_GET(self):508+ curl_str = 'curl -L -u "user:pass" -X GET -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" "https://httpbin.ep.io/basic-auth/user/pass"'509+ auth = ('user', 'pass')510+ r = requests.get(httpsbin('basic-auth', 'user', 'pass'), auth=auth)511+ self.assertEqual(curl_from_request(r.request), curl_str)512+513+514+ def test_curl_POSTBIN_GET_POST_FILES(self):515+ curl_str = 'curl -L -X POST -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" -d "some=data" "http://httpbin.org/post"' 516+ post = requests.post(httpbin('post'), data={'some': 'data'})517+ self.assertEqual(curl_from_request(post.request), curl_str)518+519+ curl_str = 'curl -L -X POST -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" -F "some=@test_requests.py" "https://httpbin.ep.io/post"' 520+ post2 = requests.post(httpsbin('post'), files={'some': open('test_requests.py')})521+ self.assertEqual(curl_from_request(post2.request), curl_str)522+523+ curl_str = 'curl -L -X POST -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" -d \'[{"some": "json"}]\' "http://httpbin.org/post"' 524+ post3 = requests.post(httpbin('post'), data='[{"some": "json"}]')525+ self.assertEqual(curl_from_request(post3.request), curl_str)526+527+528 if __name__ == '__main__':529 unittest.main()