Skip to content
Myr

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

5 files+123 added−1 removed

modified:docs/api.rst+4 added
46 unmodified lines
47.. autofunction:: dict_from_cookiejar48.. autofunction:: cookiejar_from_dict49.. autofunction:: add_dict_to_cookiejar5051Curl52~~~~53.. autofunction:: curl_from_request5455Encodings56~~~~~~~~~5758.. autofunction:: get_encodings_from_content
35 unmodified lines
modified:requests/utils.py+70 added-1 removed
214 unmodified lines
215216 :param content: bytestring to gzip-decode.217 """218219 return zlib.decompress(content, 16+zlib.MAX_WBITS)219 return zlib.decompress(content, 16+zlib.MAX_WBITS)220221222def curl_from_request(request):223 """Returns a curl command from the request.224225 :param request: The :class:`Request <Request>` object226227 Example:228 | import requests229 | from requests.utils import curl_from_request230 | r = requests.get('http://httpbin.org/get')231 | curl_from_request(r.request)232233 """234235 #TODO - OAuth236237 #: -L/--location - if there is a redirect, redo request on the new place.238 curl = 'curl -L '239240 #: -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)245246 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()253254 #: -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])260261 #: -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()])265266 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)277278 #: 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)285286 #: Params handled in _build_url287 return curl + auth + method + header + cookies + form + '"' + request._build_url() + '"'288
modified:AUTHORS+1 added
41 unmodified lines
42- Alejandro Giacometti43- Rick Mak44- Johan Bergström45- Josselin Jacquard46- Mike Waldner
modified:HISTORY.rst+1 added
4 unmodified lines
5* 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 requests10110.6.1 (2011-08-20)12++++++++++++++++++13
154 unmodified lines
modified:test_requests.py+47 added
12 unmodified lines
1314import requests1516from requests.sessions import Session17from requests.utils import curl_from_request181920HTTPBIN_URL = 'http://httpbin.org/'21HTTPSBIN_URL = 'https://httpbin.ep.io/'
455 unmodified lines
477 assert heads['User-agent'] in r2.content478 self.assertEqual(r2.status_code, 200)479480481 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)485486487 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"'489490 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)493494495 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)499500501 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)505506507 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)512513514 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)518519 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)522523 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)526527528if __name__ == '__main__':529 unittest.main()