Dart: Get POST parameters on server side

Issue

I send some data from client side using POST request

var value = new Map<String, String>();
value["param1"] = 'value1';
value["param2"] = 'value2';
value["param3"] = 'value3';

HttpRequest.postFormData('http://localhost:8080/', value);

and try to get this data on the server side:

HttpServer.bind(InternetAddress.ANY_IP_V6, 8080).then((server) {
    server.listen((HttpRequest request) {
        //TODO: process POST request
    });
});

But how can I get POST values from the request as Map< string, string>?

upd 1

But as I see result of

var jsonString = await request.transform(UTF8.decoder).join();

depends on type of post message. If I change it result will be

multipart/form-data

------WebKitFormBoundaryoQQD7N0iA5zS8qmg
Content-Disposition: form-data; name="param1"

value 1
------WebKitFormBoundaryoQQD7N0iA5zS8qmg
Content-Disposition: form-data; name="param2"

value 2
------WebKitFormBoundaryoQQD7N0iA5zS8qmg
Content-Disposition: form-data; name="param3"

value 3
------WebKitFormBoundaryoQQD7N0iA5zS8qmg--

text/plain

param1=value 1
param2=value 2
param3=value 3

application/x-www-form-urlencoded

param1=value+1&param2=value+2&param3=value+3

As I have already asked how can I convert it to Map< string, string>?

Solution

Here is a complete tutorial https://www.dartlang.org/docs/tutorials/httpserver/#handling-post

String jsonString = await request.transform(UTF8.decoder).join();

or

Map result = await request.transform(UTF8.decoder).join().then(JSON.decode);

Answered By – Günter Zöchbauer

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *