Easy HTTP
Easy HTTP is a lightweight Flutter package that simplifies HTTP requests, making it effortless to perform GET, POST, PUT, and DELETE operations. With an intuitive API, you can start making network requests right away without any complex setup.
Features
- ๐ Simple and intuitive API for HTTP requests
- ๐ง Supports GET, POST, PUT, and DELETE methods out of the box
- ๐ฏ Minimal setup required - start using it immediately
- ๐ Easy handling of JSON data
- ๐ Customizable headers and request parameters
- ๐ Multipart form uploads with fields and files
- ๐งช Optional custom
http.Clientfor tests or client reuse
Getting started
To use this package, add ez_http as a dependency in your pubspec.yaml file:
dependencies:
ez_http: ^1.0.11
Then run dart pub get or flutter pub get to install the package.
Usage
Here are some quick examples to get you started with Easy HTTP:
All request methods return Future<EasyHttpResponse<T>?>. The parsed payload is
available on response.body.
If a request or response parse still fails after the configured attempts, the
method returns null.
GET Request
import 'package:ez_http/ez_http.dart';
void main() async {
final EasyHttpResponse<Map<String, dynamic>>? response =
await EasyHttp.get<Map<String, dynamic>>(
'https://api.example.com/users',
responseBodyType: ResponseBodyType.json,
);
print(response?.body);
}
POST Request
import 'package:ez_http/ez_http.dart';
void main() async {
final EasyHttpResponse<Map<String, dynamic>>? response =
await EasyHttp.post<Map<String, dynamic>>(
'https://api.example.com/users',
body: {'name': 'John Doe', 'email': 'john@example.com'},
responseBodyType: ResponseBodyType.json,
contentType: ContentType.json,
);
print(response?.statusCode);
print(response?.body);
}
PUT Request
import 'package:ez_http/ez_http.dart';
void main() async {
final EasyHttpResponse<Map<String, dynamic>>? response =
await EasyHttp.put<Map<String, dynamic>>(
'https://api.example.com/users/1',
body: {'name': 'Jane Doe'},
responseBodyType: ResponseBodyType.json,
contentType: ContentType.json,
);
print(response?.statusCode);
}
DELETE Request
import 'package:ez_http/ez_http.dart';
void main() async {
final EasyHttpResponse<Map<String, dynamic>>? response =
await EasyHttp.delete<Map<String, dynamic>>(
'https://api.example.com/users/1',
);
print(response?.statusCode);
}
Multipart File Upload
Most apps only need one of these three options:
EasyHttpFile.bytes(...): when your file is already in memoryawait EasyHttpFile.path(...): when you already have a local file pathEasyHttpFile.text(...): when you want to upload plain text as a file
The field name defaults to file, so the simplest examples do not need to set it.
Upload Bytes
import 'package:ez_http/ez_http.dart';
void main() async {
final file = EasyHttpFile.bytes(
bytes: [1, 2, 3, 4],
filename: 'avatar.png',
);
final response = await EasyHttp.post<String>(
'https://api.example.com/upload',
body: {'userId': '42'},
files: [file],
);
print(response?.statusCode);
}
Upload A Local File Path
import 'package:ez_http/ez_http.dart';
void main() async {
final file = await EasyHttpFile.path(
path: '/path/to/avatar.png',
filename: 'avatar.png',
);
final response = await EasyHttp.post<String>(
'https://api.example.com/upload',
body: {'userId': '42'},
files: [file],
);
print(response?.statusCode);
}
Upload Text As A File
import 'package:ez_http/ez_http.dart';
void main() async {
final file = EasyHttpFile.text(
value: 'Hello from ez_http',
filename: 'note.txt',
);
final response = await EasyHttp.post<String>(
'https://api.example.com/upload',
files: [file],
);
print(response?.statusCode);
}
Body Types
ContentType.json: accepts any JSON-encodable object and sends it as JSON.ContentType.urlEncodedornull: aMapbody is sent asapplication/x-www-form-urlencoded.ContentType.formData: aMapbody is sent asmultipart/form-data, andEasyHttpmanages the multipart boundary header for you.files: passList<EasyHttpFile>. Iffilesis not empty,EasyHttpautomatically uses multipart.ContentType.plainText: sends the body as a string.
Create upload files with the matching EasyHttpFile constructor:
await EasyHttpFile.path(...): useful on mobile/desktop when you have a file path.EasyHttpFile.bytes(...): useful for web or in-memory file data.EasyHttpFile.text(...): useful for text file uploads in tests.
Multipart file uploads are sent once per call. If an upload fails, create fresh
files with the EasyHttpFile constructors before calling EasyHttp again.
Response Body Types
ResponseBodyType.rawandResponseBodyType.string: return a UTF-8 decoded string response.ResponseBodyType.json: parses JSON usingjsonDecode.ResponseBodyType.int,double,bool: parse scalar values.ResponseBodyType.binary: returnsresponse.bodyBytes.
Custom Client
You can inject a custom http.Client when you want to reuse a client instance
or provide a mock client in tests:
import 'package:ez_http/ez_http.dart';
import 'package:http/http.dart' as http;
void main() async {
final client = http.Client();
try {
final response = await EasyHttp.get<String>(
'https://api.example.com/health',
client: client,
);
print(response?.body);
} finally {
client.close();
}
}
If you pass a custom client, you are responsible for closing it. If you do
not pass one, EasyHttp creates and closes its own client internally.
Additional information
For more detailed examples and advanced usage, please check out the /example folder in the package repository.
Contributing
Contributions are welcome! If you encounter any issues or have suggestions for improvements, please file an issue on the GitHub repository.
License
This package is released under the MIT License. See the LICENSE file for details.