bodyAsUrlEncodedForm method

Future<Map<String, String>> bodyAsUrlEncodedForm({
  1. Encoding encoding = conv.utf8,
})

Decodes url-encoded form from the body and returns the form as Map<String, String>.

Example: final server = new Jaguar(); server.post('/add', (ctx) async { final Map<String, String> map = await ctx.req.bodyAsUrlEncodedForm(); // ... }); await server.serve();

Implementation

Future<Map<String, String>> bodyAsUrlEncodedForm(
    {conv.Encoding encoding = conv.utf8}) async {
  if (_parsedUrlEncodedForm != null) {
    return _parsedUrlEncodedForm!;
  }

  final String text = await bodyAsText(encoding);

  List<String> fields = text.split("&");

  final ret = <String, String>{};

  for (String field in fields) {
    List<String> parts = field.split("=");
    if (parts.length == 2)
      ret[Uri.decodeQueryComponent(parts.first)] =
          Uri.decodeQueryComponent(parts.last);
    else
      ret[Uri.decodeQueryComponent(parts.first)] = "";
  }

  _parsedUrlEncodedForm = ret;
  return ret;
}