Search code examples
xmlhttprequestdart

dart, how do you read the content body of a http request?


I am playing with dart http server and I'm not sure how to read the actual content sent in the http request: "{'text':'some text data.'}"

import 'dart:io';


void main() {

  HttpServer.bind('127.0.0.1', 3000).then((server){
    server.listen((HttpRequest request) {
      print("request made");
      request.response.write('''
        <html>
          <head>
          </head>
          <body>
            <pre>
              HELLO:
              request info:
                method: ${request.method}
                uri: ${request.uri}
                content length: ${request.contentLength}
                content : //HOW DO I GET THIS?
            </pre>
            <script>

              var req = new XMLHttpRequest();
              req.open("POST","/a_demonstration");
              req.send("{'text':'some text data.'}");

            </script>
          </body>
        </html>
      ''');
      request.response.close();
    });
  });

}

Solution

  • You can use :

    import 'dart:convert' show utf8;
    
    Future<String> content = utf8.decodeStream(request);