Remove default Content-Length from GET requests.
authorJohnny Goodnow <j.goodnow29@gmail.com>
Fri, 25 Jan 2013 05:10:12 +0000 (21:10 -0800)
committerJohnny Goodnow <j.goodnow29@gmail.com>
Fri, 25 Jan 2013 05:13:32 +0000 (21:13 -0800)
Fix #1051.

requests/models.py
test_requests.py

index 9926045..b05ef25 100644 (file)
@@ -386,13 +386,14 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
         self.body = body
 
     def prepare_content_length(self, body):
-        self.headers['Content-Length'] = '0'
         if hasattr(body, 'seek') and hasattr(body, 'tell'):
             body.seek(0, 2)
             self.headers['Content-Length'] = str(body.tell())
             body.seek(0, 0)
         elif body is not None:
             self.headers['Content-Length'] = str(len(body))
+        elif self.method in ('POST', 'PUT'):
+            self.headers['Content-Length'] = '0'
 
     def prepare_auth(self, auth):
         """Prepares the given HTTP auth data."""
index c974e98..24cbc13 100644 (file)
@@ -58,6 +58,11 @@ class RequestsTestCase(unittest.TestCase):
         assert pr.body == 'life=42'
 
 
+    def test_no_content_length(self):
+        req = requests.Request('GET', httpbin('get')).prepare()
+        self.assertNotIn('Content-Length', req.headers)
+
+
     def test_path_is_not_double_encoded(self):
         request = requests.Request('GET', "http://0.0.0.0/get/test case").prepare()