platform-common-utilities/packages/body_parser
2024-05-21 23:51:17 +08:00
..
example Added range_header, user_agent and body_parser 2021-09-12 09:23:12 +08:00
lib Upgraded to support sdk 2.17 2022-07-06 22:04:09 +08:00
test Fixed body_parser analysis warnings 2022-08-28 22:11:10 +08:00
analysis_options.yaml Added range_header, user_agent and body_parser 2021-09-12 09:23:12 +08:00
AUTHORS.md Added range_header, user_agent and body_parser 2021-09-12 09:23:12 +08:00
CHANGELOG.md Updated Changelog 2024-05-21 23:51:17 +08:00
LICENSE Added range_header, user_agent and body_parser 2021-09-12 09:23:12 +08:00
melos_belatuk_body_parser.iml Upgraded to support sdk 2.17 2022-07-06 22:04:09 +08:00
pubspec.yaml Updated to min dart sdk 3.3 2024-05-21 23:08:07 +08:00
README.md Updated to use lints 3.0.0 2023-12-12 10:10:10 +08:00

Belatuk Body Parser

Pub Version (including pre-releases) Null Safety License

Replacement of package:body_parser with breaking changes to support NNBD.

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 Angel3 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

This package is similar to Express.js's body-parser module. It fully supports JSON, x-www-form-urlencoded as well as query strings requests. You can also include arrays in your query, in the same way you would for a PHP application. 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'.

Installation

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

dependencies: belatuk_body_parser: ^5.2.0

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:belatuk_body_parser/belatuk_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 Angel3 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 = String.fromCharCodes(req.originalBuffer);
    // ...
  }
});