http_middleware

Last updated:

0 purchases

http_middleware Image
http_middleware Images
Add to Cart

Description:

http middleware

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
copied to clipboard
Importing #
import 'package:http_middleware/http_middleware.dart';
copied to clipboard
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),
]);
copied to clipboard
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/');
copied to clipboard
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),
]);
copied to clipboard
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
}
}
copied to clipboard
HttpWithMiddleware supports all the functions that http provides.
http.get(...);
http.post(...);
http.put(...);
http.delete(...);
http.head(...);
http.patch(...);
http.read(...);
http.readBytes(...);
copied to clipboard
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();
copied to clipboard
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}");
}
}
copied to clipboard
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"];
}
}
copied to clipboard
Packages built on http_middleware #

http_logger : Easy request and response logging.

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

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.