From f003025a37f9d546570e52f7d355c6f5d1a81617 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sat, 24 Nov 2012 21:47:47 -0500 Subject: [PATCH 1/3] Attach Content-Length to everything. Closes #223 --- requests/models.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/requests/models.py b/requests/models.py index 9c0666a6..84fae6e3 100644 --- a/requests/models.py +++ b/requests/models.py @@ -541,6 +541,10 @@ class Request(object): else: content_type = 'application/x-www-form-urlencoded' + self.headers['Content-Length'] = '0' + if body is not None: + self.headers['Content-Length'] = str(len(body)) + # Add content-type if it wasn't explicitly provided. if (content_type) and (not 'content-type' in self.headers): self.headers['Content-Type'] = content_type From 61f16d1ddcf4729945ce7b0b9bfbd94b75dc4b92 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sat, 24 Nov 2012 22:01:16 -0500 Subject: [PATCH 2/3] Handle files as well. --- requests/models.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/requests/models.py b/requests/models.py index 84fae6e3..4025dae7 100644 --- a/requests/models.py +++ b/requests/models.py @@ -542,7 +542,11 @@ class Request(object): content_type = 'application/x-www-form-urlencoded' self.headers['Content-Length'] = '0' - if body is not None: + if isinstance(body, file): + 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)) # Add content-type if it wasn't explicitly provided. From b93fbd30c32b122bf7aba7db6663ca5a292b17d3 Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Sat, 24 Nov 2012 22:43:52 -0500 Subject: [PATCH 3/3] Fix python 3 tests. --- requests/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requests/models.py b/requests/models.py index 4025dae7..44b41d8e 100644 --- a/requests/models.py +++ b/requests/models.py @@ -542,7 +542,7 @@ class Request(object): content_type = 'application/x-www-form-urlencoded' self.headers['Content-Length'] = '0' - if isinstance(body, file): + if hasattr(body, 'seek') and hasattr(body, 'tell'): body.seek(0, 2) self.headers['Content-Length'] = str(body.tell()) body.seek(0, 0)