我在尝试使下一个代码正常工作时遇到一些异常。
Python 2.7:
jsonDictionaryInner["message"] = message_received
jsonDictionaryInner["mac"] = repr(myMACInner)
jsonDictionaryInner["mac"] = jsonDictionaryInner["mac"].replace('\'', '')
try:
json_dumped = json.dumps(jsonDictionaryInner)
headers = {'Content-type': 'application/json', 'Authorization': myMACInner, 'Content-Length':len(json_dumped)}
if type_message == "obs":
requests.post(urlObservation, data=json_dumped, headers=headers)
如果我在发布之前打印它,json_dumped 看起来像这样:
{"message": {"measurements": [{"position": 0, "sensor": 11, "value": 26.56}, {"position": 1, "sensor": 12, "value": 90.0}, {"position": 1, "sensor": 13, "value": 23.2}], "id": 34, "observationDate": "2016-03-03 19:39:37"}, "mac": "8b:45:ed:11:77:ab\\n"}
我伪造了 Mac,但它与那个类似。
在node.js端:
app.post('/adddevice', function(req, res) {
console.log("adddevice");
addDevice(req.get('authorization'), req.body, function(data){
res.send(data);
});
});
Python 端出现异常:
('Connection aborted.', BadStatusLine("''",))
在node.js端:
SyntaxError: Unexpected end of input
at Object.parse (native)
at parse (/home/myfolder/myapi/node_modules/body-parser/lib/types/json.js:88:17)
at /home/myfolder/myapi/node_modules/body-parser/lib/read.js:116:18
at invokeCallback (/home/myfolder/myapi/node_modules/raw-body/index.js:262:16)
at done (/home/myfolder/myapi/node_modules/raw-body/index.js:251:7)
at IncomingMessage.onEnd (/home/myfolder/myapi/node_modules/raw-body/index.js:308:7)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
我在node.js端使用express和body-parser,在python中使用json和requests
最佳答案
似乎 json 负载被某种方式截断了。你能打印出 Node 在有效负载中实际接收到的内容吗?
关于python - 将带有 json 的帖子从 python 发送到 Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35780352/