Mailing List Archive

[issue14721] httplib doesn't specify content-length header for POST requests without data
New submission from Arve Knudsen <arve.knudsen@gmail.com>:

httplib doesn't specify the HTTP header 'content-length' for POST requests without data. Conceptually this makes sense, considering the empty content. However, IIS (7.5) servers don't accept such requests and respond with a 411 status code. See this question on StackOverflow for reference: http://stackoverflow.com/questions/5915131/can-i-send-an-empty-http-post-webrequest-object-from-c-sharp-to-iis.

See also issue #223 of the Requests project, https://github.com/kennethreitz/requests/issues/223, which regards this problem in the context of the requests Python library.

The following code makes a data-less POST request to the HTTP sniffer Fiddler running on localhost:
import httplib
conn = httplib.HTTPConnection("localhost", 8888)
conn.request("POST", "/post", "", {})
conn.close()

Fiddler reports that it receives the following headers for the POST request, as you can see 'content-length' is not included:
POST http://localhost:8888/post HTTP/1.1
Host: localhost:8888
Accept-Encoding: identity

----------
components: Library (Lib)
messages: 159915
nosy: Arve.Knudsen
priority: normal
severity: normal
status: open
title: httplib doesn't specify content-length header for POST requests without data
versions: Python 2.7

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Jesús Cea Avión <jcea@jcea.es> added the comment:

Could you provide a patch?

Does this affect 3.x too?

----------
nosy: +jcea

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Changes by Ezio Melotti <ezio.melotti@gmail.com>:


----------
nosy: +ezio.melotti
stage: -> test needed
type: -> behavior

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Arve Knudsen <arve.knudsen@gmail.com> added the comment:

I can look into patch and 3.x tonight I think. Should I provide a test with an eventual patch?

----------

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Jesús Cea Avión <jcea@jcea.es> added the comment:

Patch with test, please :-).

I know it is a pain in the ass, but the result is having a higher quality python.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Changes by Antoine Pitrou <pitrou@free.fr>:


----------
nosy: +orsenthil
stage: test needed -> needs patch
versions: +Python 3.2, Python 3.3

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Piotr Dobrogost <pd@bugs.python.dobrogost.net> added the comment:

> Fiddler reports that it receives the following headers for the POST request

Python 3.2.3 on Windows Vista 64bit gives the same output for

import http.client
conn = http.client.HTTPConnection('localhost',8888)
conn.request("POST", "/post", "", {})
conn.close()

----------
nosy: +piotr.dobrogost

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Changes by Éric Araujo <merwok@netwok.org>:


----------
nosy: +eric.araujo

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Arve Knudsen <arve.knudsen@gmail.com> added the comment:

Which HTTP methods should we auto-define content-length for? POST and PUT? I noticed that my first attempt at a patch would define content-length also for GET requests, which broke a unit test (test_ipv6host_header).

----------

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Arve Knudsen <arve.knudsen@gmail.com> added the comment:

Actually, when inspecting the HTTP requests sent by Chrome for the different methods (a great little Chrome app called Postman let me fire requests manually), I found that content-length would be set for most methods. I could confirm that 'content-length: 0' would be set for the following methods: POST, PUT, PATCH, DELETE and HEAD.

I guess it should be good enough to model that behaviour in httplib?

----------

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Jesús Cea Avión <jcea@jcea.es> added the comment:

HEAD?. It doesn't make sense in HEAD if it doesn't make sense in GET.

Looking around, I found this, to mud the water a little bit more: <http://fixunix.com/tcp-ip/66198-http-rfc-related-question-content-length-0-get-request.html>.

Not being explicit about this is a "bug" in the specification, I think.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Arve Knudsen <arve.knudsen@gmail.com> added the comment:

Here's my initial proposal for a patch against httplib, based on the 2.7 branch of cpython repository. I've included a couple of tests, which check that when content is empty, content-length is set to 0 for certain methods (POST/PUT etc) and not specified for others.

----------
keywords: +patch
Added file: http://bugs.python.org/file25456/httplib-2.7.patch

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue14721] httplib doesn't specify content-length header for POST requests without data [ In reply to ]
Arve Knudsen <arve.knudsen@gmail.com> added the comment:

Yes, I agree it doesn't make much sense for HEAD AFAICT, but Chrome does it. Maybe there's a reason?

----------

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue14721>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com