【问题标题】:POST from nodemcu to orion, 411 error but Content-Length exists in headers从 nodemcu 到 orion 的 POST,411 错误,但标头中存在 Content-Length
【发布时间】:2016-02-16 13:44:15
【问题描述】:

我将 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>

【问题讨论】:

    标签: json post fiware-orion nodemcu


    【解决方案1】:

    已排序!

    我在每个标题前添加了一个空格。 这是有效载荷的正确代码:

    local postval = "POST /v1/contextEntities HTTP/1.1\r\n"     
              .."Host: 130.206.124.50\r\n" 
              .."Accept: application/json\r\n"
              .."Content-Type: application/json\r\n"
              .."Content-Length: "
              ..num.."\r\n"
              .."\r\n"
              ..var.."\r\n"  
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    相关资源
    最近更新 更多