shelf_multipart 2.0.0 copy "shelf_multipart: ^2.0.0" to clipboard
shelf_multipart: ^2.0.0 copied to clipboard

shelf_multipart parses multipart and form-data requests for shelf handlers.

shelf_multipart parses multipart and multipart/form-data requests for shelf handlers.

Handling multipart requests #

Multipart requests are represented by the MultipartRequest extension type. To check whether a request is a multipart request, MultipartRequest.of or the Request.multipart() extension method can be used:

import 'package:shelf_multipart/shelf_multipart.dart';
import 'package:shelf/shelf.dart';

Future<Response> myHandler(Request request) async {
  if (request.multipart() case var multipart?) {
    // Iterate over parts making up this request:
    await for (final part in multipart.parts) {
      // Headers are available through part.headers as a map:
      final headers = part.headers;
      // part implements the `Stream<List<int>>` interface which can be used to
      // read data. You can also use `part.readBytes()` or `part.readString()`
    }
  } else {
    return Response(401); // not a multipart request
  }
}

Handling multipart/form-data #

Since form data is also sent as multipart requests, this package provides methods to read form data as well:

if (request.formData() case var form?) {
  // Read all form-data parameters into a single map:
  final parameters = <String, String>{
    await for (final formData in form.formData)
     formData.name: await formData.part.readString(),
  };
}
30
likes
160
pub points
91%
popularity

Publisher

verified publishersimonbinder.eu

shelf_multipart parses multipart and form-data requests for shelf handlers.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

http_parser, mime, shelf, string_scanner

More

Packages that depend on shelf_multipart