httpagent
httpagent is a simple and easy-to-use Dart package for making HTTP requests. It supports GET, POST, PUT, DELETE, and multipart POST requests, making it easy to handle JSON data, headers, and callback functions for task completions and errors.
✨ Features
- 🔄 GET, POST, PUT, DELETE & multipart POST
- 🧠 Smart JSON body handling
- 🧰 Global & custom headers support
- ✅ Callback-based success & error handling
- 📦 Lightweight and easy to use
Installation
Add httpagent to your pubspec.yaml file:
dependencies:
httpagent: ^latest
Then run:
flutter pub get
Usage
Import the package in your Dart file:
import 'package:httpagent/httpagent.dart';
NetworkUtils Class
The NetworkUtils class is used to perform HTTP requests. It requires a URL, a caller identifier, and a callback object that implements the NetworkResponse interface.
Example Usage
GET Request
Future<void> getData() async {
NetworkUtils task = NetworkUtils(
"https://example.com/api/users",
'getData',
this,
useDefaultHeaders: true, // Using global headers
);
await task.get();
}
POST Request
Future<void> postData() async {
NetworkUtils task = NetworkUtils(
"https://example.com/api/users",
'postData',
this,
);
await task.post(data: {'name': 'John Doe', 'email': 'john@example.com'});
}
PUT Request
Future<void> putData() async {
NetworkUtils task = NetworkUtils(
"https://example.com/api/users/1",
'putData',
this,
);
await task.put(data: {'name': 'Updated Name'});
}
DELETE Request
Future<void> deleteData() async {
NetworkUtils task = NetworkUtils(
"https://example.com/api/users/1",
'deleteData',
this,
);
await task.delete();
}
Multipart POST Request
Future<void> uploadFile() async {
NetworkUtils task = NetworkUtils(
"https://example.com/api/upload",
'uploadFile',
this,
useDefaultHeaders: true,
);
await task.multipartPost(
data: {'description': 'File Upload'},
filePath: '/path/to/file.jpg',
headers: {
'File-Token': 'SecureUpload123',
},
);
}
NetworkResponse Interface
The NetworkResponse interface defines two methods:
onTaskComplete(String result, String caller): Called when a request completes successfully.onTaskError(String error, String caller): Called when a request fails.
Implement this interface in your class to handle responses and errors.
Contributors 🙌
A huge thanks to these amazing people:
- Ankit Gauswami - Creator & Maintainer 🎯
- Nikunj - Improved API structure 🛠️
httpagent simplifies HTTP requests in Flutter and Dart, making API integration easier and more manageable! 🚀