Code Monkey home page Code Monkey logo

shelf_body_parser's Introduction

A Middleware for Shelf.

Created from templates made available by Lijian under a BSD-style license.

Usage

A simple usage example:

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_body_parser/shelf_body_parser.dart';

// Run shelf server and host a [Service] instance on port 8080.
void main() async {
  var handler = const Pipeline()
      .addMiddleware(logRequests())
      .addMiddleware(bodyParser(storeOriginalBuffer: false))
      .addHandler(_messages);
  final server = await shelf_io.serve(handler, 'localhost', 8080);
  print('Server running on localhost:${server.port}');
}

Future<Response> _messages(Request request) async {
  // View body data (need to open storeOriginalBuffer)
  print((request.context['originalBuffer'] as Buffer).store);
  // Read GET parameters
  print((request.context['query'] as Map<String, String>)['aaa']);
  // Read POST parameters (application/x-www-form-urlencoded)
  print((request.context['postParams'] as Map<String, dynamic>)['xx']);
  // Obtain POST binary stream (form-data)
  final file = new File('./ccc.png');
  IOSink fileSink = file.openWrite();
  await (request.context['postFileParams'] as Map<String, List<FileParams>>)['zzz1'][0].part.pipe(fileSink);
  fileSink.close();
  print(((request.context['postFileParams'] as Map<String, dynamic>)['yyy'] as List<String>)[0]);

  print(request.context);
  return Response.ok('[]');
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

shelf_body_parser's People

Contributors

tabula-rasa avatar unger1984 avatar

Stargazers

Duc-Thien Bui avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.