Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP headers for chunked encoding POST - Error 411

I'm sending data to a server with an Arduino which requires constructing an HTML POST line-by-line. I don't necessarily know the Content-Length a-priori, so I am using "chunked" encoding.

When I tried this example post from Wikipedia with the "Transfer-Encoding" option as specified in rfc2616

client.println("POST /myurl HTTP/1.1");
client.println("Host: 12.345.679.999"); // replaced with the test server's IP
client.println("User-Agent: Arduino/1.0");
client.println("Transfer-Encoding: chunked");
client.println();  
client.println("4");
client.println("test");
client.println("0");
client.println();

or, with escape characters explicit:

client.print("4\r\ntest\r\n0\r\n\r\n");

I receive the error from my server:

HTTP/1.1 411 Length Required
A request of the requested method POST requires a valid Content-length.
Server: Apache/2.2.22 (Ubuntu)

However, "chunked" encoding shouldn't require a Content-Length header field, see 4.4 - Message Length in rfc2616

Am I missing a field? Why doesn't this call work?

For the record, the non-Chunked-Encoding works:

if(client.connect(server, 80)){
    String PostData = "test";
    Serial.println("POST /myurl HTTP/1.1");
    client.println("Host: 12.345.679.999"); // replaced with the test server's IP
    Serial.println("User-Agent: Arduino/1.0");
    Serial.print("Content-Length: ");
    Serial.println(PostData.length());
    Serial.println();
    Serial.println(PostData);
}

UPDATE

From the apache2 error.log: "chunked Transfer-Encoding forbidden"

like image 894
Tom Wainwright Avatar asked Jun 22 '13 21:06

Tom Wainwright


People also ask

What is a 411 response code?

The 411 HTTP Status Code is sent by the server as a response when it refuses to accept a message without a content-length header, for whatever reason. A server simply may or may not accept content without a Content-Length header.

How do I enable chunked transfer encoding?

To enable chunked transfer encoding, set the value for AspEnableChunkedEncoding to True in the metabase for the site, the server, or the virtual directory that you want to enable chunked transfer encoding for. By default, the value is True, and it is set at the Web service level.

How do I stop transfer encoding chunked?

Try adding "&headers=false" to your request. That should shorten it up and cause the response to be less likely to be chunked. Also, are you sending a HTTP/1.1 or HTTP/1.0 request? Try sending a HTTP/1.0 if your device cannot handle a HTTP/1.1 request.


1 Answers

After finding

chunked Transfer-Encoding forbidden

in my Apache2 log I concluded that the error was not in the POST that I was making.

I found that modwsgi (the middle-layer between apache and django) does not enable chunked transfer-encoding by default. In the past, chunked wasn't supported at all

Refering to the change-log in the new version of modwsgi, I found that writing

WSGIChunkedRequest On

in my apache httpd.conf file allowed chunked requests (no more 411 error)

like image 81
Tom Wainwright Avatar answered Sep 16 '22 20:09

Tom Wainwright