如何修复:“烧瓶request.arg.get('language')函数返回NONE

时间:2019-10-02 10:50:49

标签: flask http-post content-type esp8266

当我尝试从Esp8266向Flask App发送HTTPRequest时, İt返回

我正在发送

  http.POST("language=python");

到烧瓶服务器。

如何在Flask服务器上获取价值?

这是我的ESP8266 HTTPRequest代码:

 void loop() {
   // wait for WiFi connection
   if ((WiFiMulti.run() == WL_CONNECTED)) {
     WiFiClient client;

     HTTPClient http;

     Serial.print("[HTTP] begin...\n");
     if (http.begin(client, "http://192.168.1.107:8090/query")) {  // HTTP


     http.addHeader("Content-Type", "application/x-www-form-urlencoded");      
     int httpCode = http.POST("language=python");
     String payload = http.getString();
     Serial.println(httpCode);   //Print HTTP return code
     Serial.println(payload);         



     http.end();
   } else {
     Serial.printf("[HTTP} Unable to connect\n");
   }

这是我的烧瓶代码:

 from flask import Flask,render_template
 from flask import request

 app = Flask(__name__)

 @app.route('/query',methods=['GET','POST'])
 def helloHandler():
     language = request.get_data() #if key doesn't exist, returns None
     veri=request.args.get('language')

     print(request.get_data()) 
     print(veri)

     return '''<h1>The language value is: {}</h1>'''.format(veri)

 app.run(host='0.0.0.0', port= 8090)

这是程序运行时的结果:

https://ibb.co/C5YZN59

1 个答案:

答案 0 :(得分:0)

看看基础的werkzeug请求data方法。它会调用get_data,它似乎对您有用:

@cached_property
def data(self):
    """
    Contains the incoming request data as string in case it came with
    a mimetype Werkzeug does not handle.
    """

    if self.disable_data_descriptor:
        raise AttributeError("data descriptor is disabled")
    # XXX: this should eventually be deprecated.
    # We trigger form data parsing first which means that the descriptor
    # will not cache the data that would otherwise be .form or .files
    # data.  This restores the behavior that was there in Werkzeug
    # before 0.9.  New code should use :meth:`get_data` explicitly as
    # this will make behavior explicit.
    return self.get_data(parse_form_data=True)