2017-07-19 21:20:57 +00:00
|
|
|
# body_parser
|
|
|
|
[![Pub](https://img.shields.io/pub/v/body_parser.svg)](https://pub.dartlang.org/packages/body_parser)
|
2017-10-12 02:32:42 +00:00
|
|
|
[![build status](https://travis-ci.org/angel-dart/body_parser.svg)](https://travis-ci.org/angel-dart/body_parser)
|
2016-03-04 04:30:13 +00:00
|
|
|
|
2016-04-17 19:51:40 +00:00
|
|
|
Parse request bodies and query strings in Dart, as well multipart/form-data uploads. No external
|
|
|
|
dependencies required.
|
2016-03-04 04:30:13 +00:00
|
|
|
|
2017-07-19 21:20:57 +00:00
|
|
|
This is the request body parser powering the
|
|
|
|
[Angel](https://angel-dart.github.io)
|
|
|
|
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!
|
|
|
|
|
2016-03-04 04:30:13 +00:00
|
|
|
### Contents
|
|
|
|
|
|
|
|
* [Body Parser](#body-parser)
|
|
|
|
* [About](#about)
|
|
|
|
* [Installation](#installation)
|
|
|
|
* [Usage](#usage)
|
2016-03-04 04:36:45 +00:00
|
|
|
* [Thanks](#thank-you-for-using-body-parser)
|
2016-03-04 04:30:13 +00:00
|
|
|
|
|
|
|
# About
|
|
|
|
|
|
|
|
I needed something like Express.js's `body-parser` module, so I made it here. It fully supports JSON requests.
|
2016-04-17 03:11:33 +00:00
|
|
|
x-www-form-urlencoded fully supported, as well as query strings. You can also include arrays in your query,
|
2016-04-17 19:51:40 +00:00
|
|
|
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.
|
2016-03-04 04:30:13 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-07-19 21:20:57 +00:00
|
|
|
Body Parser exposes a simple class called `BodyParseResult`.
|
2016-03-04 04:30:13 +00:00
|
|
|
You can easily parse the query string and request body for a request by calling `Future<BodyParseResult> parseBody`.
|
|
|
|
|
|
|
|
```dart
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-03-04 04:35:59 +00:00
|
|
|
You can also use `buildMapFromUri(Map, String)` to populate a map from a URL encoded string.
|
|
|
|
|
2016-03-04 04:30:13 +00:00
|
|
|
This can easily be used with a library like [JSON God](https://github.com/thosakwe/json_god)
|
|
|
|
to build structured JSON/REST APIs. Add validation and you've got an instant backend.
|
|
|
|
|
|
|
|
```dart
|
|
|
|
MyClass create(HttpRequest request) async {
|
|
|
|
return god.deserialize(await parseBody(request).body, MyClass);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-07-19 21:20:57 +00:00
|
|
|
## 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`.
|
2016-03-04 04:30:13 +00:00
|
|
|
|
2017-07-19 21:20:57 +00:00
|
|
|
For example, if you wanted to
|
|
|
|
[parse GraphQL queries within your server](https://github.com/angel-dart/graphql)...
|
2016-03-04 04:30:13 +00:00
|
|
|
|
2017-07-19 21:20:57 +00:00
|
|
|
```dart
|
|
|
|
app.get('/graphql', (req, res) async {
|
|
|
|
if (req.headers.contentType.mimeType == 'application/graphql') {
|
|
|
|
var graphQlString = new String.fromCharCodes(req.originalBuffer);
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|