用于分块编码 POST 的 HTTP header - 错误 411

标签 http http-headers arduino http-post http-error

我正在使用 Arduino which requires constructing an HTML POST line-by-line 向服务器发送数据.我不一定知道先验内容长度,所以我使用的是“分块”编码。

当我尝试 this example post from Wikipedia"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();

或者,使用明确的转义字符:

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

我从我的服务器收到错误:

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

但是,“分块”编码不应该要求 Content-Length header 字段,参见 4.4 - Message Length in rfc2616

我是否缺少一个字段?为什么这个调用不起作用?

郑重声明,非分块编码有效:

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);
}

更新

来自 apache2 error.log:“禁止分块传输编码”

最佳答案

找到之后

chunked Transfer-Encoding forbidden

在我的 Apache2 日志中,我得出结论,错误不在我正在制作的 POST 中。

我发现 modwsgi(apache 和 django 之间的中间层)默认情况下不启用分块传输编码。 In the past, chunked wasn't supported at all

Refering to the change-log in the new version of modwsgi , 我发现写作

WSGIChunkedRequest On

在我的 apache httpd.conf 文件中允许分块请求(不再有 411 错误)

关于用于分块编码 POST 的 HTTP header - 错误 411,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17255556/

相关文章:

javascript - Http 请求在主体中返回比预期更多的数据

node.js - Node 中的 API 请求返回错误

c++ - 使用cpp-netlib 0.8作为HTTP代理——无奈

http - 如何在golang中为http header 中的主机与url中的主机设置不同的值

arduino - Arduino Uno 可以将数据发送到 Cloud Firestore 吗?

c# - 由c#程序控制的Arduino设备

java - 为下载管理器添加 HTTPS 支持

f# - 如何为 Web 请求指定 header

php - PHP 是否不受 "HTTP Response Splitting"漏洞的影响?

build - PlatformIO 致命构建错误 : LiquidCrystal. h "No such file or directory"