Merge branch 'curl_feature' of https://github.com/mawaldne/requests into feature/curl
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 724ff7e, 2aa32c1 · Browse files · 3 files·+69 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+69 added−8 removed
- other:+69 added−8 removed
Provenance
- Commit
b6aa868- Tree
2cfc530- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
724ff7e- Parent 2
2aa32c1
3 files changed+69 added−8 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+18 added-8 removed
167 unmodified lines168 return zlib.decompress(content, 16+zlib.MAX_WBITS)169 170 171 def curl_from_request(request):172- """Creates a curl command from the request."""172+ """Returns a curl command from the request.173 174+ :param request: The :class:`Request <Request>` object175+176+ Example:177+ | import requests178+ | from requests.utils import curl_from_request179+ | r = requests.get('http://httpbin.org/get')180+ | curl_from_request(r.request)181+182+ """183+184 #TODO - OAuth185 176- #: -L/--location - if there is a redirect, redo request on the new place186+ #: -L/--location - if there is a redirect, redo request on the new place.187 curl = 'curl -L '188 189 #: -u/--user - Specify the user name and password to use for server auth.190 #: Basic Auth only for now2 unmodified lines193 auth = '-u "%s:%s" ' % (request.auth.username, request.auth.password)194 195 method = ''196 if request.method.upper() == 'HEAD':187- #: -I/--head - fetch headers only197+ #: -I/--head - fetch headers only.198 method = '-I '199 else:190- #: -X/--request - specify request method200+ #: -X/--request - specify request method.201 method = '-X %s ' % request.method.upper()202 203 #: -b/--cookie204 #: (HTTP) Pass the data to the HTTP server as a cookie. It is supposedly the1 unmodified line206 cookies = ''207 if request.cookiejar:208 cookies = cookies.join(['-b "%s=%s" ' % (k.name, k.value) for k in request.cookiejar])209 200- #: -H/--header - Extra header to use when getting a web page210+ #: -H/--header - Extra header to use when getting a web page.211 header = ''212 if request.headers:213 header = header.join(['-H "%s:%s" ' % (k, v) for k, v in request.headers.iteritems()])214 215 form = ''216 if request.method in ('PUT', 'POST', 'PATCH'):207- #: request.files is updated with request.data if both exist.208- #: ContentType multipart/form-data is used217+ #: request.files is updated with request.data if both exist, so only iterate request.files.218+ #: ContentType multipart/form-data is used.219 if request.files:220 #: -F/--form - Emulate form data. To force 'content' to a file, prefix file name @.221 for k, v in request.files.iteritems():222 if isinstance(v, file):223 form = form + '-F "%s=@%s" ' % (k, v.name)224 elif v not in (None, ''):225 form = form + '-F "%s=%s" ' % (k, v)226 217- #: content-type application/x-www-form-urlencoded is used here227+ #: content-type application/x-www-form-urlencoded is used here.228 else:229 #: -d/--data - send specified data in post request.230 if isinstance(request.data, (list, tuple)):231 form = form.join(['-d "%s=%s" ' % (k, v) for k, v in request.data])6 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/'448 unmodified lines470 assert heads['User-agent'] in r2.content471 self.assertEqual(r2.status_code, 200)472 473 474+ def test_curl_HTTP_OK_GET(self):475+ curl_str = 'curl -L -X GET -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" "http://httpbin.org//"'476+ r = requests.get(httpbin('/'))477+ self.assertEqual(curl_from_request(r.request), curl_str)478 479+480+ def test_curl_HTTP_OK_GET_WITH_PARAMS(self):481+ curl_str = 'curl -L -X GET -H "Accept-Encoding:gzip" -H "User-agent:Mozilla/5.0" "http://httpbin.org/user-agent"'482+483+ heads = {'User-agent': 'Mozilla/5.0'}484+ r = requests.get(httpbin('user-agent'), headers=heads)485+ self.assertEqual(curl_from_request(r.request), curl_str)486+487+488+ def test_curl_HTTP_OK_HEAD(self):489+ curl_str ='curl -L -I -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" "http://httpbin.org//"'490+ r = requests.head(httpbin('/'))491+ self.assertEqual(curl_from_request(r.request), curl_str)492+493+494+ def test_curl_HTTP_OK_PATCH(self):495+ curl_str = 'curl -L -X PATCH -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" "http://httpbin.org/patch"'496+ r = requests.patch(httpbin('patch'))497+ self.assertEqual(curl_from_request(r.request), curl_str)498+499+500+ def test_curl_AUTH_HTTPS_OK_GET(self):501+ 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"'502+ auth = ('user', 'pass')503+ r = requests.get(httpsbin('basic-auth', 'user', 'pass'), auth=auth)504+ self.assertEqual(curl_from_request(r.request), curl_str)505+506+507+ def test_curl_POSTBIN_GET_POST_FILES(self):508+ curl_str = 'curl -L -X POST -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" -d "some=data" "http://httpbin.org/post"' 509+ post = requests.post(httpbin('post'), data={'some': 'data'})510+ self.assertEqual(curl_from_request(post.request), curl_str)511+512+ 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"' 513+ post2 = requests.post(httpsbin('post'), files={'some': open('test_requests.py')})514+ self.assertEqual(curl_from_request(post2.request), curl_str)515+516+ curl_str = 'curl -L -X POST -H "Accept-Encoding:gzip" -H "User-Agent:python-requests.org" -d \'[{"some": "json"}]\' "http://httpbin.org/post"' 517+ post3 = requests.post(httpbin('post'), data='[{"some": "json"}]')518+ self.assertEqual(curl_from_request(post3.request), curl_str)519+520+521 if __name__ == '__main__':522 unittest.main()