This should take care of #1266
authorIan Cordasco <graffatcolmingov@gmail.com>
Thu, 28 Mar 2013 03:26:11 +0000 (23:26 -0400)
committerIan Cordasco <graffatcolmingov@gmail.com>
Thu, 28 Mar 2013 03:26:11 +0000 (23:26 -0400)
We were sending 'None' as the Content-Length on requests where the body was a
generator. This commit should prevent that entirely.

requests/models.py

index fe384a1..1451800 100644 (file)
@@ -349,7 +349,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
         ])
 
         try:
-            length = str(super_len(data))
+            length = super_len(data)
         except (TypeError, AttributeError):
             length = False
 
@@ -360,7 +360,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
                 raise NotImplementedError('Streamed bodies and files are mutually exclusive.')
 
             if length:
-                self.headers['Content-Length'] = length
+                self.headers['Content-Length'] = str(length)
             else:
                 self.headers['Transfer-Encoding'] = 'chunked'
         # Check if file, fo, generator, iterator.
@@ -392,7 +392,9 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
             self.headers['Content-Length'] = str(body.tell())
             body.seek(0, 0)
         elif body is not None:
-            self.headers['Content-Length'] = str(len(body))
+            l = super_len(body)
+            if l:
+                self.headers['Content-Length'] = super_len(l)
         elif self.method not in ('GET', 'HEAD'):
             self.headers['Content-Length'] = '0'