【问题标题】:Flask Application - argument of type 'NoneType' is not iterable [duplicate]Flask Application - 'NoneType' 类型的参数不可迭代 [重复]
【发布时间】:2019-08-10 05:50:28
【问题描述】:

我写了一个小烧瓶应用程序。 我将带有 json 数据(python 脚本)的请求发送到烧瓶服务器(ubuntu 18.04 和 python 3.6.7), 和服务器评估这些数据...

客户:

data =  {   'serial': '12345'
            'printerConfig':  {
                    'virtualPrinter1':    'physicalPrinter1',
                    'virtualPrinter2':    'physicalPrinter2',
                    'virtualPrinter3':    'physicalPrinter3'
         }
}
r = requests.get("http://127.0.0.1/webservice", json=data)

服务器:

@app.route('/', methods=['GET', 'POST'])
def webservice():
if request.method == 'GET':
    serial = request.json['serial'] if 'serial' in request.json else None
    printerConfig = request.json['printerConfig'] if 'printerConfig' in request.json else None

我已经在开发服务器的 pyCharm 中测试了这段代码,并且一直运行良好。

之后,我尝试将其实现到 Apache2 服务器。 现在我在 apache-error.log 中收到以下错误消息。

ERROR:flask.app:Exception on / [GET]
Traceback (most recent call last):
    ...
    File "/var/www/html/webservice/webservice.py", line xx, in webservice
        serial = request.json['serial'] if 'serial' in request.json else None
TypeError: argument of type 'NoneType' is not iterable

request.json 的打印只显示无。 开发服务器上正在运行完全相同的代码。

我也尝试了 request.data 而不是 request.json,结果相同。

Apache2 有专门的设置吗? 有人有想法吗?

最好的问候 浮动

【问题讨论】:

    标签: python json flask apache2.4


    【解决方案1】:

    您正在发出获取请求。

    尝试向您的网络服务发出 post 请求

    r = requests.post("http://127.0.0.1/webservice", json=data)
    

    您永远不会在获取时包含 json=""... 仅用于将 json 发布/更新到网络服务器

    请记住,http 术语中的 GET 仅用于从 URL 端点请求内容。 POST 是将新内容发送到 URL 端点。 GET/POST

    另一个快速点...尝试在您的后端/烧瓶应用程序上打印 request.json 在 serial = request.json['serial'] if 'serial' in request.json else None 等之前...只是为了确保数据在发布后正确到达。

    【讨论】:

    • 感谢您的回答。我已将 GET 更改为 POST,不幸的是结果相同:-(
    猜你喜欢
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2011-10-04
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多