我正在尝试从NodeMCU(ESP-12)生成并发送到同一LAN上的Flask服务器(PC)的HTTP POST中接收数据。服务器控制台正确显示传入的POST和响应,但POST主体上的数据在服务器端不存在。
服务器IP:192.168.0.19
ESP-12 IP:192.168.0.16
ESP-12发送的POST请求:
POST /newLog HTTP/1.1
Host: 192.168.0.19:8002
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
count=5&device=0
这是来自ESP-12的串行日志(连接请求 - 响应 - 断开连接):
connecting to 192.168.0.19
Requesting: POST /newLog HTTP/1.1
Host: 192.168.0.19:8002
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
count=5&device=0
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 18
Server: Werkzeug/0.12.2 Python/2.7.6
Date: Tue, 14 Nov 2017 12:59:24 GMT
ok.. Log received!
closing connection
这是Flask服务器上的端点(显示请求信息):
@app.route('/newLog', methods = ['POST'])
def newLog():
if request.method == 'POST':
app.logger.info("fue un POST")
app.logger.info(request.form)
app.logger.info(request.args)
app.logger.info(request.values)
app.logger.info(request.data)
return render_template('ok.html')
return render_template('fail.html')
来自服务器的日志:
-------------------------------------------------------
INFO in app [app.py:96]:
fue un POST
-------------------------------------------------------
-------------------------------------------------------
INFO in app [app.py:97]:
ImmutableMultiDict([])
-------------------------------------------------------
-------------------------------------------------------
INFO in app [app.py:98]:
ImmutableMultiDict([])
-------------------------------------------------------
-------------------------------------------------------
INFO in app [app.py:99]:
CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([])])
-------------------------------------------------------
-------------------------------------------------------
INFO in app [app.py:100]:
-------------------------------------------------------
192.168.0.16 - - [14/Nov/2017 09:59:36] "POST /newLog HTTP/1.1" 200 -
看起来请求已经到达,但是烧瓶没有找到任何数据。
当我尝试使用Postman时,POST非常相似:
POST /newLog HTTP/1.1
Host: localhost:8002
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: 79566598-b1b9-de1b-4bf9-e53d38edb232
count=5&device=0
服务器确实收到了数据:
-------------------------------------------------------
INFO in app [app.py:96]:
fue un POST
---------------------------------------------------------
---------------------------------------------------------
INFO in app [app.py:97]:
ImmutableMultiDict([('device', u'0'), ('count', u'5')])
---------------------------------------------------------
---------------------------------------------------------
INFO in app [app.py:98]:
ImmutableMultiDict([])
---------------------------------------------------------
---------------------------------------------------------
INFO in app [app.py:99]:
CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([('device', u'0'), ('count', u'5')])])
---------------------------------------------------------
---------------------------------------------------------
INFO in app [app.py:100]:
---------------------------------------------------------
127.0.0.1 - - [14/Nov/2017 10:31:07] "POST /newLog HTTP/1.1" 200 -
请帮忙!