body_parser 1.0.3

  • README.md
  • 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. Depend on it

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


dependencies:
  body_parser: "^1.0.3"

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 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.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
1.0.0-dev Apr 17, 2016 Go to the documentation of body_parser 1.0.0-dev Download body_parser 1.0.0-dev archive

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Feb 3, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

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

Platforms

Detected platforms: Flutter, other

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

Suggestions

  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use body_parser.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
http_server >=0.9.6 <1.0.0 0.9.7
Transitive dependencies
mime 0.9.6
path 1.5.1
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