1

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>?

  • I'm not sure I understand what you ask for in your update. Does my answer answer your original question? Looks like your update is a follow-up question about how to convert `multipart/form-data`, `text/plain`, `application/x-www-form-urlencoded` request data to a map. Is that right? – Günter Zöchbauer Nov 23 '15 at 14:17
  • Your original answer provides how get request string but not a map. When I used the PHP, there was $_POST associative array which contains all post values for all post types. Can I convert the post request to a map like $_POST array in the PHP? –  Nov 23 '15 at 16:02
  • There is no such associative array in Dart you can use for all types. I updated my answer with the JSON result for the original question. – Günter Zöchbauer Nov 23 '15 at 16:28
  • I also want to mention, that IMHO there is much less requirement in Dart for that. Dart is for SPAs and usually only JSON or protouf is sent. Form posts usually produce a page reload which is usually not what you want in a SPA. – Günter Zöchbauer Nov 23 '15 at 16:38

2 Answers2

0

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);
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
0

I have the same question, and I didn't find any solution until now. I need to pass a map from client to server, and server to use that map to interrogate a mongodb database. Client send that map, but server receive a string. Any conversion to json return also string, not a map. On the client side I send a map named query:

await HttpRequest.postFormData('http://localhost:8085/$_coll',query).then((HttpRequest response) 

On the server side :

if (request.method == 'POST') {

   query = await request.transform(utf8.decoder).join();
}

I've tried to encode/decode to json, but with no success.

user310563
  • 13
  • 3