DART & GAE : Why a POST method send from dart can&

2019-07-26 12:28发布

I have a Dart code used to send an HttpRequest with a POST method to my GAE WepApp2 application. The dart code is executed in chromium and serve by Chrome dev editor. I add in my GAE code some headers to avoid the XHR error in the client side.

The dart code send the datas to my GAE app but I can't read the data with self.request.POST.get("language")) and the app never enter in def post(self): section but with self.request.body I can read the data.

Could you explain that and provide some correction to have a full POST compliant code?

dart:

void _saveData() {
    HttpRequest request = new HttpRequest(); // create a new XHR

    // add an event handler that is called when the request finishes
    request.onReadyStateChange.listen((_) {
      if (request.readyState == HttpRequest.DONE &&
          (request.status == 200 || request.status == 0)) {
        // data saved OK.
        print(request.responseText);
      }
    });

    // POST the data to the server
    var url = "http://127.0.0.1:8080/savedata";
    request.open("POST", url, async: false);

    String jsonData = JSON.encode({"language":"dart"});
    request.send(jsonData);
  }

GAE code in my handler:

  def savedata(self):
    logging.info("test")
    logging.info(self.request.body)
    logging.info(self.request.POST.get("language"))
    def post(self):
      logging.info("test 2")
      logging.info(self.request.POST.get("language"))

    self.response.headers["Access-Control-Allow-Origin"] = "http://127.0.0.1:49981"
    self.response.headers["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"

1条回答
看我几分像从前
2楼-- · 2019-07-26 13:03

In Dart, if you don't specify request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") in your HttpRequest, the data is considered by GAE like a bite stream and you can only read them with self.request.body

If you add the Content-Type header in Dart you need also to change the data formating. In my case I mimic a form sending with POST method so I change String jsonData = JSON.encode({"language":"dart"}); by String jsonData = "language=dart2";

IN GAE python I can now read the data with self.request.POST.get("language")

If you need to send a JSON from DART to GAE, you can encode the string like this:

String jsonData = JSON.encode({"test":"valuetest1"});
String datas = "datas=$jsonData";
request.send(datas);

In GAE you can read the datas like this:

my_json = json.loads(self.request.POST.get("datas"))
logging.info(my_json["test"])

The complete code:

Dart

void _saveData2() {
    String url = "http://127.0.0.1:8080/savedata";
    HttpRequest request = new HttpRequest() 
      ..open("POST", url, async: true)
      ..setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
      ..responseType = "arraybuffer";

    String jsonData = JSON.encode({"test":"valuetest1"});
    String datas = "datas=$jsonData";
    request.send(datas);
  }

GAE

class PageHandler(webapp2.RequestHandler):

  def savedata(self):

    self.response.headers.add_header('Access-Control-Allow-Origin', '*')
    self.response.headers['Content-Type'] = 'application/json'

    #logging.info(self.request)
    my_json = json.loads(self.request.POST.get("datas"))
    logging.info(my_json["test"])
查看更多
登录 后发表回答