Flask Application-类型'NoneType'的参数不可迭代

时间:2019-03-19 16:11:46

标签: python json flask apache2.4

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

客户:

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是否有特殊设置? 有人知道吗?

最好的问候 弗洛

1 个答案:

答案 0 :(得分:0)

您正在获取请求。

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

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

进行获取...时,您绝不会包含json =“”,仅用于将json发布/更新到网络服务器

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

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