Adding cookies
authorMike Waldner <mwaldner@gilt.com>
Fri, 26 Aug 2011 03:55:24 +0000 (23:55 -0400)
committerMike Waldner <mwaldner@gilt.com>
Fri, 26 Aug 2011 03:55:24 +0000 (23:55 -0400)
#139

requests/utils.py

index 03e5fac400aca6971a5ed1af4c57488b25b63546..8a0967b9f30839cdf93c6110f299c4a3e89c6897 100644 (file)
@@ -172,7 +172,6 @@ def curl_from_request(request):
     """Creates a curl command from the request."""
 
     #TODO - OAuth
-    #TODO - Cookies
 
     #: -L/--location - if there is a redirect, redo request on the new place
     curl = 'curl -L '
@@ -183,6 +182,7 @@ def curl_from_request(request):
     if request.auth is not None:
        auth = '-u "%s:%s" ' % (request.auth.username, request.auth.password)
 
+    method = ''
     if request.method.upper() == 'HEAD':
         #: -I/--head - fetch headers only
         method = '-I '
@@ -190,6 +190,13 @@ def curl_from_request(request):
         #: -X/--request - specify request method
         method = '-X %s ' % request.method.upper()
 
+    #: -b/--cookie
+    #: (HTTP) Pass the data to the HTTP server as a cookie. It is supposedly the
+    #: data previously received from the server in a "Set-Cookie:" line. 
+    cookies = ''
+    if request.cookiejar:
+        cookies = cookies.join(['-b "%s=%s" ' % (k.name, k.value) for k in request.cookiejar]) 
+
     #: -H/--header - Extra header to use when getting a web page
     header = ''
     if request.headers:
@@ -216,5 +223,5 @@ def curl_from_request(request):
                 form = "-d '%s' " % (request._enc_data)
 
     #: Params handled in _build_url
-    return curl + auth + method + header + form + '"' + request._build_url() + '"'
+    return curl + auth + method + header + cookies + form + '"' + request._build_url() + '"'