httpagent 0.1.2 copy "httpagent: ^0.1.2" to clipboard
httpagent: ^0.1.2 copied to clipboard

httpagent is a comprehensive library of custom HTTP API calling for Flutter.

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 #

  • Global Default Headers: Set headers that apply to all requests using ApiHeaders.setDefaultHeaders.
  • Flexible Requests: Easily perform GET, POST, PUT, DELETE, and multipart POST requests.
  • Custom Headers Per Request: Merge global headers with extra headers or ignore defaults completely.
  • JSON Handling: Automatically encode request bodies to JSON.
  • Callback Functions: Implement the NetworkResponse interface to handle successful responses and errors.

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 - Creator & Maintainer 🎯
  • Nikunj - Improved API structure 🛠️

httpagent simplifies HTTP requests in Flutter and Dart, making API integration easier and more manageable! 🚀

9
likes
0
points
12
downloads

Publisher

unverified uploader

Weekly Downloads

httpagent is a comprehensive library of custom HTTP API calling for Flutter.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, http

More

Packages that depend on httpagent