body_parser 1.1.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 73

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.

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.0

  • Add parseBodyFromStream

example/main.dart

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

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

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

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

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

  HttpServer.bind(address, port, shared: true).then((server) {
    server.listen((request) async {
      var body = await parseBody(request);
      request.response
        ..headers.contentType = ContentType.JSON
        ..write(JSON.encode(body.body))
        ..close();
    });

    print(
        '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:


dependencies:
  body_parser: "^1.1.0"

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.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
1.0.0-dev+1 Sep 24, 2016 Go to the documentation of body_parser 1.0.0-dev+1 Download body_parser 1.0.0-dev+1 archive

All 11 versions...

Analysis

We analyzed this package on Jun 12, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.60.0
  • pana: 0.11.3

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
50 / 100
Health:
Code health derived from static analysis. [more]
93 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
73
Learn more about scoring.

Platforms

Detected platforms: Flutter, other

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

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 12 hints.

    Run dartfmt to format lib/src/body_parse_result.dart.

    Run dartfmt to format lib/src/chunk.dart.

    Similar analysis of the following files failed:

    • lib/src/file_upload_info.dart (hint)
    • lib/src/get_value.dart (hint)
    • lib/src/parse_body.dart (hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
http_parser >=3.1.1 <4.0.0 3.1.2
http_server >=0.9.6 <1.0.0 0.9.7
mime >=0.9.3 <1.0.0 0.9.6+1
Transitive dependencies
charcode 1.1.1
collection 1.14.10
path 1.6.0
source_span 1.4.0
string_scanner 1.0.2
typed_data 1.1.5
Dev dependencies
http >=0.11.3 <0.12.0
json_god >=2.0.0-beta <3.0.0
test >=0.12.15 <0.13.0