Node-Red接收响应为JSON

时间:2016-02-20 09:13:31

标签: python node.js node-red

这是我在Node-Red中的流程

enter image description here

我只是将一个字符串注入到我的服务器的tcp节点,然后我的服务器将该值作为字符串返回。但是,我继续从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响应(检查上面的调试结果),任何想法为什么?

1 个答案:

答案 0 :(得分:0)

问题在于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
  • 第一个更改是将标题
  • 中的','换成','
  • 其次是在请求行中的URL和标题之间添加None