HTTP请求中的永久NULL-body错误使用HTTP模块

时间:2018-01-05 16:44:17

标签: lua nodemcu

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

-- BME280 Setup
sda, scl = 3, 4
sleep_time = 9000000
i2c.setup(0, sda, scl, i2c.SLOW) -- call i2c.setup() only once
bme280.setup()
-- BME280 Read
--T, P, H = bme280.read()


-- Analog read
adc.force_init_mode(adc.INIT_ADC)
--I = adc.read(0)

-- Send data to Orange Pi
--payload = {"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',
  function(code, data)
    if (code < 0) then
      print("HTTP request failed")
    else
      print(code, data)
      print("Sent to the web OK")
    end
  end) 


-- Deep Sleep
--node.dsleep(sleep_time)

我经常收到&#34; HTTP客户端的错误:正文不应该为空&#34;。即使使用文档中的示例。

我查看了模块的源代码,我对一段代码感到困惑:

if (NULL == body) {
      /* Find missing body */
      HTTPCLIENT_ERR("Body shouldn't be NULL");
      /* To avoid NULL body */
      body = "";
} else {
      /* Skip CR & LF */
      body = body + 4;
}

由于某种原因,if总是如此。比较操作中参数的顺序是否有差异?或者在另一个地方发生错误?

1 个答案:

答案 0 :(得分:2)

根据the documentation,正文是第三个参数。

  

语法

     

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

您需要将第二个参数(标题)添加为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 request failed")
    else
      print(code, data)
      print("Sent to the web OK")
    end
  end)