我将 json 对象中的这 2 个值(temp 和 humi)发送到我的 orion 服务器,但它失败并显示 orion 错误消息:
<orionError>
<code>411</code>
<reasonPhrase>Length Required</reasonPhrase>
<details>Zero/No Content-Length in PUT/POST/PATCH request</details>
</orionError>
尽管包含“Content-Length:”标头(请参见下面的代码)。我现在迷路了...
我的代码(在 ESPlorer v0.2.0rc2 中执行)是:
humi=10
temp=20
function postValores(level)
connout = nil
connout = net.createConnection(net.TCP, 0)
connout:on("receive", function(connout, payloadout)
print (payloadout);
if (string.find(payloadout, "400 Bad Request") ~= nil) then
print("BAD POST");
end
if (string.find(payloadout, "411 Bad Request") ~= nil) then
print("BAD POST");
end
if (string.find(payloadout, "200 OK") ~= nil) then
print("GOOD POST");
end
end)
connout:on("connection", function(connout, payloadout)
print ("Posting...")
local var = '{"id": "Sala1", "type": "Sala", "attributes": [{"name": "temperature", "type": "float","value": "'..temp..'" },{"name": "humidity", "type": "float", "value": "'..humi..'"}]}'
local num = string.len(var)
local postval = "POST /v1/contextEntities/ HTTP/1.1\r\n"
.." Host: 130.206.124.50\r\n"
.." Content-Length: "
..num.."\r\n"
.." Content-Type: application/json\r\n"
.." Accept: application/json\r\n"
.."\r\n"
..var.."\r\n"
print (postval)
connout:send(postval)
end)
connout:on("disconnection", function(connout, payloadout)
connout:close()
collectgarbage()
end)
connout:connect(1026,'130.206.124.50')
end
tmr.alarm(2, 16000, 1, function() postValores(0) end)
我发送:
Posting...
POST /v1/contextEntities/ HTTP/1.1
Host: 130.206.124.50
Content-Length: 157
Content-Type: application/json
Accept: application/json
{"id": "Sala1", "type": "Sala", "attributes": [{"name": "temperature", "type": "float","value": "20" },{"name": "humidity", "type": "float", "value": "10"}]}
回应是:
HTTP/1.1 411 Length Required
Content-Length: 163
Content-Type: application/xml
Date: Tue, 16 Feb 2016 13:43:12 GMT
<orionError>
<code>411</code>
<reasonPhrase>Length Required</reasonPhrase>
<details>Zero/No Content-Length in PUT/POST/PATCH request</details>
</orionError>