【问题标题】:Node-Red receiving response as JSONNode-Red 以 JSON 形式接收响应
【发布时间】:2016-02-20 09:13:30
【问题描述】:

这是我在 Node-Red 中的流程

我只是将一个字符串注入到我的服务器的 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 响应(检查上面的调试结果),知道为什么吗?

【问题讨论】:

  • 你实际上并没有在这里问过问题
  • 您使用的是什么版本的 Node-RED?测试 v0.13.2 和 curl 我得到了预期的纯文本有效负载。 (附注:添加一个 http-out 节点,以便 Node-RED 实际上以 200 响应帖子)
  • 顺便说一句 - 我也在你在这里问的地方回答了这个问题:developer.ibm.com/answers/questions/254167/…

标签: python node.js node-red


【解决方案1】:

问题出在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

【讨论】:

  • 非常感谢。我不知道我是如何以及为什么用逗号写的。花了我们的时间来调试我眼前的问题。我也会在那里标记正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多