enhanced-http

Pub GitHub license

A wrapper around the dart http package provided by Google (https://pub.dev/packages/http) which is inspired by axios

Getting started

Create an instance of enhanced http as follows :

EnhancedHttp http = EnhancedHttp(baseURL: "https://dog.ceo/api");

Custom headers can be provided through the name parameter 'headers' ( Default content type is specified as application/json ) :

EnhancedHttp http = EnhancedHttp(
  baseURL: "https://dog.ceo/api",
  headers: {'Authorization': "Bearer $token"}
);

Provide optional interceptors as follows :

EnhancedHttp http = EnhancedHttp(
    baseURL: "https://dog.ceo/api",
    interceptors: InterceptorOptions(
      response: (dynamic res) {
        print("Status ${res["status"]}");
        print("Headers ${res["headers"]}");
        print("Data ${res["data"]}");
        return res["data"];
      },
      error: (dynamic e) {
        print(e);
        return "An error has occurred please try again later";
      }
    )
);

Usage

Fetch data from an api endpoint - GET

final res = await http.get("/path");

Send data to an api endpoint - POST

final res = await http.post("/path", payload: {
    "data": "This is some sample data to send to a server"
});

Update data at an api endpoint - PUT

final res = await http.put("/path", payload: {
    "data": "This is some sample data to update at a server"
});

Partially update data at an api endpoint - PATCH

final res = await http.put("/path", payload: {
    "data": "This is some sample data to update at a server"
});

Delete data at an api endpoint - DELETE

final res = await http.delete("/path");

Request headers at an api endpoint - HEAD

final res = await http.head("/path");

Additional Parameters

The files attribute on the http post, http put and http patch methods can be used to send files to the server

final res = await http.post("/path",
    payload: {
        "data": "This is some sample data to update at a server"
    },
    headers: {
      "Content-Type": "multipart/form-data"
    },
    files: [
        {
            "array_key": "file",
            "file": File("path_to_file")
        }
    ],
);

Access to the underlying http package

The underlying http package can be accessed as follows bypassing the transitive dependency import warning:

import 'package:enhanced_http/enhanced_http.dart' as http;

final res = await http.get("/path");