Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2022-05-30
16:25 • Closed ticket [6afa947d1a]: http::geturl : Missing Content-Length header for POS... artifact: 85931128bd user: kjnash
2022-05-13
03:01 • Pending ticket [6afa947d1a]. artifact: e8d38f659e user: kjnash
2022-05-12
01:40 • Ticket [6afa947d1a]: 3 changes artifact: da440bfcf5 user: kjnash
2022-05-10
22:05 • Ticket [6afa947d1a]: 3 changes artifact: 70a273226d user: kjnash
2017-06-05
20:09 • New ticket [6afa947d1a]. artifact: f6b4cabc8a user: droll

http::geturl : Missing Content-Length header for POST requests

Closed / Fixed6afa947d1a92aacecdc4e681741d006b63da4e94Created 9 years agoUpdated 5 years ago
Descriptionplain2017-06-05 20:09:20
HTTP POST requests require a Content-Length header if they don't use chunked encoding.

The http::geturl command doesn't provide the Content-Length header if it performs a POST requests with an empty body/query. This is certainly a specific situation, however HTTP post requests with empty body/query are legal requests. However, a missing Content-Length header may create misunderstandings on the server side (e.g. seen with Mongoose 6.4).

The following code lines demonstrate that the Content-Length header is missing for POST requests if the body/query is missing or empty. The code redefines the 'puts' command to print the data transferred through the socket on the standard output.

package require http
#-> 2.8.9

rename puts puts_orig
proc puts {args} {
	::puts_orig stdout "#Std -> [lindex $args end]"
	::puts_orig {*}$args
}

http::geturl http://192.168.1.21:8083/JS/Run/zway.controller.data.softwareRevisionVersion.value -method POST
#Std -> POST /JS/Run/zway.controller.data.softwareRevisionVersion.value HTTP/1.1
#Std -> Host: 192.168.1.21:8083
#Std -> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 10.0) http/2.8.9 Tcl/8.6.4
#Std -> Connection: close
#Std -> Accept: */*
#Std -> Accept-Encoding: gzip,deflate,compress

http::geturl http://192.168.1.21:8083/JS/Run/zway.controller.data.softwareRevisionVersion.value -method POST -query ""
#Std -> POST /JS/Run/zway.controller.data.softwareRevisionVersion.value HTTP/1.1
#Std -> Host: 192.168.1.21:8083
#Std -> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 10.0) http/2.8.9 Tcl/8.6.4
#Std -> Connection: close
#Std -> Accept: */*
#Std -> Accept-Encoding: gzip,deflate,compress

http::geturl http://192.168.1.21:8083/JS/Run/zway.controller.data.softwareRevisionVersion.value -method POST -query "Hello"
#Std -> POST /JS/Run/zway.controller.data.softwareRevisionVersion.value HTTP/1.1
#Std -> Host: 192.168.1.21:8083
#Std -> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 10.0) http/2.8.9 Tcl/8.6.4
#Std -> Connection: close
#Std -> Accept: */*
#Std -> Accept-Encoding: gzip,deflate,compress
#Std -> Content-Type: application/x-www-form-urlencoded
#Std -> Content-Length: 5
#Std -> 
#Std -> Hello

It would be good to fix the http::geturl command in a way that the 'Content-Length' header is also provided for POST requests with empty/missing bodies/queries.
kjnashwiki2022-05-10 22:05:17

Hi Andreas,

You appear to be using "-method POST" to work around the issue that http::geturl treats "-query {}" as a GET not a POST (see also bug [15e4bd83c28d0a]).

The man page http(n) states:

    -method type
        Force the HTTP request method to type. ::http::geturl will auto-select GET, POST or HEAD based on other options, but this option enables choices like PUT and DELETE for webdav support.

The option -method allows the caller to override the method that would otherwise be selected by http::geturl. This option gives the script the power to send nonsense requests. For example, the options "-query foo -method GET" will send a GET with a request body, although RFC 7231 states:

   A payload within a GET request message has no defined semantics;
   sending a payload body on a GET request might cause some existing
   implementations to reject the request.

When using option -method, it is the responsibility of the caller to choose arguments such that the request is not nonsense. In general it is not possible for http::geturl to guess what the caller intends. If there were no other way to send a POST without a request body, I would attempt to adapt http::geturl. However, there is a solution and so I propose to amend the man page to clarify the use of option -method and mention this specific case:

It is the caller's responsibility to ensure that the headers and request body (if any) conform to the requirements of the method.  For example if using -method POST to send a POST with an empty request body, the caller must also supply the option "-headers {Content-Length 0}".

Best wishes,

Keith Nash.

kjnashwiki2022-05-12 01:40:23

Documentation http(n) revised in commit [751f947bc87f6b2f].

kjnash Pending ticket [6afa947d1a]. [History diff]2022-05-13 03:01:22
kjnash Closed ticket [6afa947d1a]. [History diff]2022-05-30 16:25:31
Log in (anonymous is fine) to add a comment.