我尝试在Fiware中执行POST请求时出错

时间:2019-03-25 10:27:22

标签: post request fiware

我正在开发一个脚本,以向Fiware的Orion Context Broker发出POST请求。问题是a收到错误:

  

“ ContextLengthRequired”

,但请求中有Context-Length标头。

这是我尝试节点fiware.js时的错误

{
  "error":"ContentLengthRequired",
  "description":"Zero/No Content-Length in PUT/POST/PATCH request"
}

这是文件 fiware.js 中的代码:

var request = require('request');

var entity = {
  "id": "Room6",
  "type": "Room",
  "temperature": {
    "value": 23,
    "type": "Float"
  },
  "pressure": {
    "value": 700,
    "type": "Float"
  }
};

var jsonObject = JSON.stringify(entity);
var aux = jsonObject.toString();
aux = aux.length;

var peticion = {
  url: "http://127.0.0.1:1026/v2/entities",
  method: "POST",
  headers: {
    "Content-Length": aux,
    "Content-Type": "application/json"
  },
  data: jsonObject
};

request(peticion, function(error, response, body) {
  console.log(error);
  console.log(body);
});

1 个答案:

答案 0 :(得分:0)

从此更改变量peticion:

  

var peticion = {     网址:“ http://127.0.0.1:1026/v2/entities”,     方法:“ POST”,     标头:{       “内容长度”:辅助,       “ Content-Type”:“ application / json”     },     数据:jsonObject   };

对此:

  

var peticion = {     网址:“ http://127.0.0.1:1026/v2/entities”,     方法:“ POST”,     标头:{       “内容长度”:辅助,       “ Content-Type”:“ application / json”     },      body :jsonObject   };

相关问题