使用HTTP模块出现Permanent NULL-body错误的HTTP请求

我在使用WeMos D1 mini上的最新主版本NodeMCU。我正在尝试调试一个简单的脚本,以将传感器数据发送到服务器,使用HTTP-post请求。

-- BME280设置
sda,scl = 34
sleep_time = 9000000
i2c.setup(0, sda, scl, i2c.SLOW) - 仅调用一次i2c.setup()
bme280.setup() 
-- BME280读取
--T,P,H = bme280.read()

--模拟读取
adc.force_init_mode(adc.INIT_ADC)
--I = adc.read(0)

--向Orange Pi发送数据
--负载= {“sensor”:“outdoor”,“t”:T/100,“p”:P/1000,“h”:H/1000,“l”:I}

http.post(“http://orangepipc2.local/cgi-bin/data_coll.py”,'Content-Type:application/x-www-form-urlencoded\r \n''sensor = test&t=23&p=10001&h=33&l=500'functioncodedataif(code<0) then
      print(“HTTP请求失败”)
    else
      print(code,data)
      print(“成功发送到网络”)
    end
  end--深度睡眠
--node.dsleep(sleep_time)

但我一直得到一个“_HTTP客户端:正文不应该为空_”的错误。即使使用文档中的示例也是如此。

我查看了模块的源代码,并被代码片段所困扰:

ifNULL == body){
      /*找到缺少的正文*/
      HTTPCLIENT_ERR(“正文不应为空”);
      /*为了避免空主体*/
      body =“”;
} else {
      /*跳过CR \u0026 LF*/
      body = body + 4;
}

if(为某种原因,NULL == body),一直返回true。比较操作的参数顺序有差异吗?或者其他地方有错误吗?

点赞
用户794749
用户794749

根据文档,body 是第三个参数。

语法

http.post(url, headers, body, callback)

你需要将第二个参数 (headers) 添加为 nil:

http.post("http://orangepipc2.local/cgi-bin/data_coll.py", nil, 'sensor=test&t=23&p=10001&h=33&l=500',
  function(code, data)
    if (code < 0) then
      print("HTTP 请求失败")
    else
      print(code, data)
      print("已成功发送至网络")
    end
  end)
2018-01-06 02:18:57