platform/packages/body_parser/README.md

78 lines
3.5 KiB
Markdown
Raw Normal View History

2021-06-20 13:29:23 +00:00
# Angel3 Body Parser
2021-06-21 01:27:19 +00:00
2021-09-12 05:10:53 +00:00
[![version](https://img.shields.io/badge/pub-v2.1.2-brightgreen)](https://pub.dartlang.org/packages/angel3_body_parser)
2021-06-20 13:29:23 +00:00
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
2016-03-04 04:30:13 +00:00
2021-06-20 13:29:23 +00:00
[![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/body_parser/LICENSE)
2016-03-04 04:30:13 +00:00
2021-09-12 05:10:53 +00:00
**DEPRECATED: Replaced by [`belatuk_body_parser`](https://pub.dartlang.org/packages/belatuk_body_parser) package**
2021-06-20 13:29:23 +00:00
Parse request bodies and query strings in Dart, as well multipart/form-data uploads. No external dependencies required.
2021-07-10 02:17:05 +00:00
This is the request body parser powering the [Angel3 framework](https://github.com/dukefirehawk/angel). 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!
2017-07-19 21:20:57 +00:00
2021-06-21 01:27:19 +00:00
## Contents
2016-03-04 04:30:13 +00:00
2021-06-21 01:27:19 +00:00
- [Angel3 Body Parser](#angel3-body-parser)
- [Contents](#contents)
- [About](#about)
- [Installation](#installation)
- [Usage](#usage)
- [Custom Body Parsing](#custom-body-parsing)
2016-03-04 04:30:13 +00:00
2021-06-21 01:27:19 +00:00
### About
2016-03-04 04:30:13 +00:00
2021-06-21 01:27:19 +00:00
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.
2016-03-04 04:30:13 +00:00
2021-06-21 01:27:19 +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.
2016-03-04 04:30:13 +00:00
2021-06-21 01:27:19 +00:00
### Installation
2016-03-04 04:30:13 +00:00
2021-06-21 01:27:19 +00:00
To install Body Parser for your Dart project, simply add body_parser to your pub dependencies.
2016-03-04 04:30:13 +00:00
dependencies:
2021-06-22 08:22:18 +00:00
angel3_body_parser: ^2.1.0
2016-03-04 04:30:13 +00:00
2021-06-21 01:27:19 +00:00
### Usage
2016-03-04 04:30:13 +00:00
2021-06-21 01:27:19 +00:00
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`.
2016-03-04 04:30:13 +00:00
```dart
import 'dart:convert';
2021-06-20 13:29:23 +00:00
import 'package:angel3_body_parser/angel3_body_parser.dart';
2016-03-04 04:30:13 +00:00
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.
2021-06-20 13:29:23 +00:00
This can easily be used with a library like [Angel3 JSON God](https://pub.dev/packages/angel3_json_god) to build structured JSON/REST APIs. Add validation and you've got an instant backend.
2016-03-04 04:30:13 +00:00
```dart
MyClass create(HttpRequest request) async {
return god.deserialize(await parseBody(request).body, MyClass);
}
```
2021-06-21 01:27:19 +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
2021-06-20 13:29:23 +00:00
For example, if you wanted to [parse GraphQL queries within your server](https://github.com/dukefirehawk/graphql_dart)...
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') {
2021-06-20 13:29:23 +00:00
var graphQlString = String.fromCharCodes(req.originalBuffer);
2017-07-19 21:20:57 +00:00
// ...
}
});
2021-06-21 01:27:19 +00:00
```