The Protevus Platform: Unified Full-Stack Development https://protevus.com
Find a file
2016-09-24 14:30:01 -04:00
.idea/libraries Multipart support thanks to http_server 2016-09-24 14:30:01 -04:00
lib Multipart support thanks to http_server 2016-09-24 14:30:01 -04:00
test Multipart support thanks to http_server 2016-09-24 14:30:01 -04:00
.gitignore Single file uploads = complete 2016-04-17 15:51:40 -04:00
LICENSE Initial commit 2016-03-03 21:32:12 -05:00
pubspec.yaml Multipart support thanks to http_server 2016-09-24 14:30:01 -04:00
README.md Multipart support thanks to http_server 2016-09-24 14:30:01 -04:00

Body Parser

version 1.0.0-dev+1

NOT YET PRODUCTION READY

Parse request bodies and query strings in Dart, as well multipart/form-data uploads. No external dependencies required.

Contents

About

I needed something like Express.js's body-parser module, so I made it here. It fully supports JSON requests. x-www-form-urlencoded fully supported, as well as query strings. You can also include arrays in your query, in the same way you would for a PHP application. Full file upload support will also be present by the production 1.0.0 release.

A benefit of this is that primitive types are automatically deserialized correctly. As in, if you have a hello=1.5 request, then body['hello'] will equal 1.5 and not '1.5'. A very semantic difference, yes, but it relieves stress in my head.

Installation

To install Body Parser for your Dart project, simply add body_parser to your pub dependencies.

dependencies:
    body_parser: any

Usage

Body Parser exposes a simple class called [BodyParseResult]. You can easily parse the query string and request body for a request by calling Future<BodyParseResult> parseBody.

import 'dart:convert';
import 'package:body_parser/body_parser.dart';

main() async {
    // ...
    await for (HttpRequest request in server) {
      request.response.write(JSON.encode(await parseBody(request).body));
      await request.response.close();
    }
}

You can also use buildMapFromUri(Map, String) to populate a map from a URL encoded string.

This can easily be used with a library like JSON God to build structured JSON/REST APIs. Add validation and you've got an instant backend.

MyClass create(HttpRequest request) async {
    God god = new God();
    return god.deserialize(await parseBody(request).body, MyClass);
}

Thank you for using Body Parser

Thank you for using this library. I hope you like it.

Feel free to follow me on Twitter:

@wapaa