All Projects → TEDConsulting → http_middleware

TEDConsulting / http_middleware

Licence: MIT License
A middleware library for Dart's http library.

Programming Languages

dart
5743 projects

Projects that are alternatives of or similar to http middleware

flutter sliding tutorial
User onboarding library with smooth animation of objects and background colors
Stars: ✭ 127 (+234.21%)
Mutual labels:  dart-library, flutter-plugin, flutter-examples, dart2
swipedetector
A Flutter package to detect up, down, left, right swipes.
Stars: ✭ 34 (-10.53%)
Mutual labels:  flutter-plugin, flutter-apps, flutter-examples, dart2
flutter-app
Full Feature Todos Flutter Mobile app with fireStore integration.
Stars: ✭ 138 (+263.16%)
Mutual labels:  flutter-plugin, flutter-apps, flutter-examples, dart2
Awesome Flutter
An awesome list that curates the best Flutter libraries, tools, tutorials, articles and more.
Stars: ✭ 38,582 (+101431.58%)
Mutual labels:  flutter-plugin, flutter-apps, flutter-examples
Flutter Learning
🔥 👍 🌟 ⭐ ⭐⭐ Flutter all you want.Flutter install,flutter samples,Flutter projects,Flutter plugin,Flutter problems,Dart codes,etc.Flutter安装和配置,Flutter开发遇到的难题,Flutter示例代码和模板,Flutter项目实战,Dart语言学习示例代码。
Stars: ✭ 4,941 (+12902.63%)
Mutual labels:  flutter-plugin, flutter-apps, flutter-examples
flutter bolg manage
Flutter实战项目,采用Getx框架管理,遵循Material design设计风格,适合您实战参考或练手
Stars: ✭ 373 (+881.58%)
Mutual labels:  flutter-plugin, flutter-apps, flutter-examples
WhatsAppUIClone
WhatsApp UI Clone with Flutter
Stars: ✭ 66 (+73.68%)
Mutual labels:  flutter-apps, flutter-examples, dart2
Flutter-Apps
🌀 This is mainly focus on a complete application for production
Stars: ✭ 18 (-52.63%)
Mutual labels:  flutter-plugin, flutter-apps, flutter-examples
cryptoplease-dart
Dart and Flutter apps and libraries maintained by Espresso Cash (Formerly Crypto Please) team for Solana.
Stars: ✭ 188 (+394.74%)
Mutual labels:  dart-library, flutter-apps, dart2
Motion-Tab-Bar
A beautiful animated flutter widget package library. The tab bar will attempt to use your current theme out of the box, however you may want to theme it.
Stars: ✭ 237 (+523.68%)
Mutual labels:  dart-library, flutter-plugin, flutter-apps
barcode.flutter
barcode generate library for Flutter
Stars: ✭ 58 (+52.63%)
Mutual labels:  dart-library, flutter-plugin, flutter-examples
flutter-tunein
Dynamically themed Music Player built with flutter
Stars: ✭ 108 (+184.21%)
Mutual labels:  flutter-plugin, flutter-apps, flutter-examples
Gumao-Flutter
Gumao: A Game Character Collector App built using Flutter
Stars: ✭ 35 (-7.89%)
Mutual labels:  flutter-apps, flutter-examples, dart2
aarogya seva
A beautiful 😍 covid-19 app with self - assessment and more.
Stars: ✭ 118 (+210.53%)
Mutual labels:  flutter-apps, flutter-examples
sticker-chat
Sticker chat is a messaging application built using Flutter, Stream, and Rive
Stars: ✭ 45 (+18.42%)
Mutual labels:  flutter-apps, flutter-examples
crypton
A simple Dart library for asymmetric encryption and digital signatures
Stars: ✭ 25 (-34.21%)
Mutual labels:  dart-library, dart2
pubspec-version
A CLI tool to get/set/bump the `version` key in pubspec.yaml.
Stars: ✭ 25 (-34.21%)
Mutual labels:  dart-library, dart2
BlogApp
A Blog App UI made with Flutter
Stars: ✭ 47 (+23.68%)
Mutual labels:  flutter-apps, flutter-examples
Bank-profile
a flutter Ui profile for bank account
Stars: ✭ 33 (-13.16%)
Mutual labels:  flutter-apps, flutter-examples
cross connectivity
A Flutter plugin for handling Connectivity and REAL Connection state in the mobile, web and desktop platforms. Supports iOS, Android, Web, Windows, Linux and macOS.
Stars: ✭ 27 (-28.95%)
Mutual labels:  flutter-plugin, dart2

http_middleware

A middleware library for Dart http library.

Getting Started

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

Installing

Include this library in your package.

http_middleware: any

Importing

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 = HttpWithMiddleware.build(middlewares: [
    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 http.post('https://jsonplaceholder.typicode.com/posts/',
    body: {"testing", "1234"});

//Simple GET request
var response = await http.get('https://jsonplaceholder.typicode.com/posts/');

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 = HttpWithMiddleware.build(
  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('https://jsonplaceholder.typicode.com/posts/');
} catch(e) {
 if (e is TimeoutException) {
   //Timed out
 }
}

HttpWithMiddleware supports all the functions that http provides.

http.get(...);
http.post(...);
http.put(...);
http.delete(...);
http.head(...);
http.patch(...);
http.read(...);
http.readBytes(...);

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 = HttpClientWithMiddleware.build(middlewares: [
    HttpLogger(logLevel: LogLevel.BODY),
]);

var response = await httpClient.post('https://jsonplaceholder.typicode.com/posts/',
    body: {"testing", "1234"});

var response = await httpClient.get('https://jsonplaceholder.typicode.com/posts/');

//Don't forget to close the client once done.
httpClient.close();

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 {
  @override
  interceptRequest({RequestData data}) {
    print("Method: ${data.method}");
    print("Url: ${data.url}");
    print("Body: ${data.body}");
  }

  @override
  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 {
  @override
  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,
    });
  }

  @override
  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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].