body_parser 1.1.1

  • Example
  • Installing
  • Versions
  • 82

body_parser #

Pub build status

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

This is the request body parser powering the Angel framework. If you are looking for a server-side solution with dependency injection, WebSockets, and more, then I highly recommend it as your first choice. Bam!

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.

    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 {
    return god.deserialize(await parseBody(request).body, MyClass);

Custom Body Parsing #

In cases where you need to parse unrecognized content types, body_parser won't be of any help to you on its own. However, you can use the originalBuffer property of a BodyParseResult to see the original request buffer. To get this functionality, pass storeOriginalBuffer as true when calling parseBody.

For example, if you wanted to parse GraphQL queries within your server...

app.get('/graphql', (req, res) async {
  if (req.headers.contentType.mimeType == 'application/graphql') {
    var graphQlString = new String.fromCharCodes(req.originalBuffer);
    // ...

1.1.1 #

  • Dart 2 updates; should fix Angel in Travis.

1.1.0 #

  • Add parseBodyFromStream


import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:body_parser/body_parser.dart';

main() async {
  var address = '';
  var port = 3000;
  var futures = <Future>[];

  for (int i = 1; i < Platform.numberOfProcessors; i++) {
    futures.add(Isolate.spawn(start, [address, port, i]));

  Future.wait(futures).then((_) {
    print('All instances started.');
        'Test with "wrk -t12 -c400 -d30s -s ./example/post.lua http://localhost:3000" or similar');
    start([address, port, 0]);

void start(List args) {
  var address = new InternetAddress(args[0] as String);
  int port = args[1], id = args[2];

  HttpServer.bind(address, port, shared: true).then((server) {
    server.listen((request) async {
      // ignore: deprecated_member_use
      var body = await parseBody(request);
        ..headers.contentType = new ContentType('application', 'json')

        'Server #$id listening at http://${server.address.address}:${server.port}');

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  body_parser: ^1.1.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:body_parser/body_parser.dart';
Version Uploaded Documentation Archive
1.1.1 Aug 11, 2018 Go to the documentation of body_parser 1.1.1 Download body_parser 1.1.1 archive
1.1.0 Mar 4, 2018 Go to the documentation of body_parser 1.1.0 Download body_parser 1.1.0 archive
1.0.3 Oct 12, 2017 Go to the documentation of body_parser 1.0.3 Download body_parser 1.0.3 archive
1.0.2 Jul 19, 2017 Go to the documentation of body_parser 1.0.2 Download body_parser 1.0.2 archive
1.0.1 Mar 7, 2017 Go to the documentation of body_parser 1.0.1 Download body_parser 1.0.1 archive
1.0.0 Mar 7, 2017 Go to the documentation of body_parser 1.0.0 Download body_parser 1.0.0 archive
1.0.0-dev+5 Jan 14, 2017 Go to the documentation of body_parser 1.0.0-dev+5 Download body_parser 1.0.0-dev+5 archive
1.0.0-dev+4 Dec 22, 2016 Go to the documentation of body_parser 1.0.0-dev+4 Download body_parser 1.0.0-dev+4 archive
1.0.0-dev+3 Dec 22, 2016 Go to the documentation of body_parser 1.0.0-dev+3 Download body_parser 1.0.0-dev+3 archive
1.0.0-dev+2 Dec 5, 2016 Go to the documentation of body_parser 1.0.0-dev+2 Download body_parser 1.0.0-dev+2 archive

All 12 versions...

Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 23, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14


Detected platforms: Flutter, other

Primary library: package:body_parser/body_parser.dart with components: io.

Health suggestions

Fix lib/src/parse_body.dart. (-2.48 points)

Analysis of lib/src/parse_body.dart reported 5 hints:

line 16 col 30: Use = to separate a named parameter from its default value.

line 36 col 30: Use = to separate a named parameter from its default value.

line 65 col 13: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

line 139 col 7: Don't explicitly initialize variables to null.

line 142 col 14: Don't explicitly initialize variables to null.

Fix lib/src/file_upload_info.dart. (-1 points)

Analysis of lib/src/file_upload_info.dart reported 2 hints:

line 16 col 58: Use = to separate a named parameter from its default value.

line 16 col 71: Use ; instead of {} for empty constructor bodies.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
dart2_constant ^1.0.0 1.0.2+dart2
http_parser >=3.1.1 <4.0.0 3.1.3
http_server >=0.9.6 <1.0.0 0.9.8+1
mime >=0.9.3 <1.0.0 0.9.6+2
Transitive dependencies
charcode 1.1.2
collection 1.14.11
meta 1.1.7
path 1.6.2
source_span 1.5.5
string_scanner 1.0.4
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
http >=0.11.3 <0.12.0
test >=0.12.15