Node-Red 接收响应 JSON
Node-Red receiving response as JSON
这是我在 Node-Red 中的流程
我只是将一个字符串注入到我的服务器的一个 tcp 节点中,然后我的服务器 returns 该值作为一个字符串。但是,我不断收到来自 Node-Red 的 JSON 响应,如下所示 {"hello":""} where the value is empty and key is hello
.
代码:
while True:
#Receiving from client
data = conn.recv(4096)
all_data += data
print 'Received Message : ' + all_data
if not data:
print 'Final Received Message : ' + all_data
headers = {'Content-Type','text/plain'}
req = urllib2.Request('<my server link>', headers)
try:
urllib2.urlopen(req, str(all_data))
except urllib2.HTTPError, error:
print "error posting the request " + error.reason
break
更新:
即使在我的代码中我返回了一个字符串,我仍然在 Node-Red 中收到 JSON 响应(检查上面的调试结果),知道为什么吗?
问题出在python,应该是这样的:
while True:
#Receiving from client
data = conn.recv(4096)
all_data += data
print 'Received Message : ' + all_data
if not data:
print 'Final Received Message : ' + all_data
headers = {'Content-Type':'text/plain'}
req = urllib2.Request('<my server link>', None, headers)
try:
urllib2.urlopen(req, str(all_data))
except urllib2.HTTPError, error:
print "error posting the request " + error.reason
break
- 第一个更改是将 headers
中的“,”替换为“:”
- 其次是在请求行的 URL 和 headers 之间添加
None
。
这是我在 Node-Red 中的流程
我只是将一个字符串注入到我的服务器的一个 tcp 节点中,然后我的服务器 returns 该值作为一个字符串。但是,我不断收到来自 Node-Red 的 JSON 响应,如下所示 {"hello":""} where the value is empty and key is hello
.
代码:
while True:
#Receiving from client
data = conn.recv(4096)
all_data += data
print 'Received Message : ' + all_data
if not data:
print 'Final Received Message : ' + all_data
headers = {'Content-Type','text/plain'}
req = urllib2.Request('<my server link>', headers)
try:
urllib2.urlopen(req, str(all_data))
except urllib2.HTTPError, error:
print "error posting the request " + error.reason
break
更新: 即使在我的代码中我返回了一个字符串,我仍然在 Node-Red 中收到 JSON 响应(检查上面的调试结果),知道为什么吗?
问题出在python,应该是这样的:
while True:
#Receiving from client
data = conn.recv(4096)
all_data += data
print 'Received Message : ' + all_data
if not data:
print 'Final Received Message : ' + all_data
headers = {'Content-Type':'text/plain'}
req = urllib2.Request('<my server link>', None, headers)
try:
urllib2.urlopen(req, str(all_data))
except urllib2.HTTPError, error:
print "error posting the request " + error.reason
break
- 第一个更改是将 headers 中的“,”替换为“:”
- 其次是在请求行的 URL 和 headers 之间添加
None
。