http_middleware 1.0.0

  • Installing
  • Versions
  • 81


A middleware library for Dart http library.

Getting Started

http_middleware is a module that lets you build middleware for Dart's http package.


Include this library in your package.

http_middleware: any


import 'package:http_middleware/http_middleware.dart';

Using http_middleware

Create an object of HttpWithMiddleware by using the build factory constructor.

The build constructor takes in a list of middlewares that are built for http_middleware. One very nice middleware you can use is the http_logger package. We will use that here for demonstration.

(You can also build your own middleware for http_middleware. Check out Build your own middleware)
HttpWithMiddleware http = [
    HttpLogger(logLevel: LogLevel.BODY),

That is it! Now go ahead and use this http object as you would normally do.

//Simple POST request
var response = await'',
    body: {"testing", "1234"});

//Simple GET request
var response = await http.get('');

Request Timout

With http_middleware you can also specify the timeout of requests. So if you want a request to be timeout in 30 seconds:

HttpWithMiddleware http =
  requestTimeout: Duration(seconds: 30),
  middlewares: [
    HttpLogger(logLevel: LogLevel.BODY),

You need to catch the exception thrown to know if connection timed out.

try {
  var response = await http.get('');
} catch(e) {
  if (e is TimeoutException) {
    //Timed out

HttpWithMiddleware supports all the functions that http provides.


Using a Client

If you want to use a http.Client in order to keep the connection alive with the server, use HttpClientWithMiddleware.

HttpClientWithMiddleware httpClient = [
    HttpLogger(logLevel: LogLevel.BODY),

var response = await'',
    body: {"testing", "1234"});

var response = await httpClient.get('');

//Don't forget to close the client once done.

Building your own middleware

Building your own middleware with http_middleware is very easy, whether you want to create a package for http_middleware or you want to build a middleware solely for your own project.

Once you have the necessary imports, all you have to do is extend the MiddlewareContract class which will give you access to 2 functions.

interceptRequest(RequestData) is the method called before any request is made. interceptResponse(ResponseData) is the method called after the response from request is received.

You can then @override all the required functions you need to add middleware to.

Example (A simple logger that logs data in all requests):

class Logger extends MiddlewareContract {
  interceptRequest({RequestData data}) {
    print("Method: ${data.method}");
    print("Url: ${data.url}");
    print("Body: ${data.body}");

  interceptResponse({ResponseData data}) {
    print("Status Code: ${data.statusCode}");
    print("Method: ${data.method}");
    print("Url: ${data.url}");
    print("Body: ${data.body}");
    print("Headers: ${data.headers}");

You can also modify the RequestData before the request is made and every ResponseData after every response is received. For Example, if you want to wrap your data in a particular structure before sending, or you want every request header to have Content-Type set to application/json.

class Logger extends MiddlewareContract {
  interceptRequest({RequestData data}) {
    //Adding content type to every request
    data.headers["Content-Type"] = "application/json";
    data.body = jsonEncode({
      uniqueId: "some unique id",
      data: data.body,

  interceptResponse({ResponseData data}) {
    //Unwrapping response from a structure
    data.body = jsonDecode(data.body)["data"];

Packages built on http_middleware

If your package uses http_middleware, open an issue and tell me, i will be happy to add it to the list.

[0.0.1] - TODO: Add release date.

  • TODO: Describe initial release.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  http_middleware: ^1.0.0

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:http_middleware/http_middleware.dart';
Version Uploaded Documentation Archive
1.0.0 Aug 28, 2018 Go to the documentation of http_middleware 1.0.0 Download http_middleware 1.0.0 archive
0.0.9 Jul 24, 2018 Go to the documentation of http_middleware 0.0.9 Download http_middleware 0.0.9 archive
0.0.8 Jul 24, 2018 Go to the documentation of http_middleware 0.0.8 Download http_middleware 0.0.8 archive
0.0.7 Jul 23, 2018 Go to the documentation of http_middleware 0.0.7 Download http_middleware 0.0.7 archive
0.0.6 Jul 23, 2018 Go to the documentation of http_middleware 0.0.6 Download http_middleware 0.0.6 archive
0.0.5 Jul 19, 2018 Go to the documentation of http_middleware 0.0.5 Download http_middleware 0.0.5 archive
0.0.4 Jul 19, 2018 Go to the documentation of http_middleware 0.0.4 Download http_middleware 0.0.4 archive
0.0.3 Jul 19, 2018 Go to the documentation of http_middleware 0.0.3 Download http_middleware 0.0.3 archive
0.0.2 Jul 19, 2018 Go to the documentation of http_middleware 0.0.2 Download http_middleware 0.0.2 archive
0.0.1 Jul 19, 2018 Go to the documentation of http_middleware 0.0.1 Download http_middleware 0.0.1 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/http_client_with_middleware.dart. (-1 points)

Analysis of lib/http_client_with_middleware.dart reported 2 hints:

line 105 col 44: 'typed' is deprecated and shouldn't be used.

line 107 col 44: 'typed' is deprecated and shouldn't be used.

Fix lib/middleware_contract.dart. (-0.50 points)

Analysis of lib/middleware_contract.dart reported 1 hint:

line 1 col 8: Unused import: 'dart:typed_data'.

Fix lib/models/response_data.dart. (-0.50 points)

Analysis of lib/models/response_data.dart reported 1 hint:

line 1 col 8: Unused import: 'dart:convert'.

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use http_middleware.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
http ^0.11.3+16 0.11.3+17 0.12.0
Transitive dependencies
async 2.0.8
charcode 1.1.2
collection 1.14.11
http_parser 3.1.3
meta 1.1.6
path 1.6.2
source_span 1.4.1
string_scanner 1.0.4
typed_data 1.1.6