simple_http_client 0.9.9

  • Readme
  • Changelog
  • Installing
  • 55

simple_web_client #

A flexible Http Client for Flutter and Dart Server. Built on top of dart:io, SimpleHttpClient adds many features that are commonly used like interceptors or authenticators.

Examples #

Making requests using simple_web_client is very straightforward.


To perform a GET request you can simply do:

SimpleHttpClient client = new SimpleHttpClient();

Response response = await client.get("");


POST is very similar to GET, but requires sending a String of the body.

SimpleHttpClient client = new SimpleHttpClient();
Response response =  await"", "{\"name\": \"asd\"}");

Headers #

The four supported methods allow you to optionally specify the headers that will be sent.

SimpleHttpClient client = new SimpleHttpClient();

Response response = await client.get("",
    headers: {"x-test": "helloWorld"});

UserAgent #

You can define the userAgent of your application by using the optional parameter of the constructor with the same name.

SimpleHttpClient client = new SimpleHttpClient(userAgent: "myAwesomeApp");

Request Interceptors #

Often you want to intercept the requests before they are dispatched. For example, to set a recurring header. Notice that the interceptors will be called in the same order in which they are added to the client.

SimpleHttpClient client = new SimpleHttpClient();

client.addInterceptor((HttpClientRequest request) async {
  request.headers.set("X-Api-Token", "mySecretToken");

// This request will be intercepted
Request request = await client.get("");

Authenticator #

This is a special interceptor that will be called upon a request receiving a 401 response from the server. If the server returns a 401 and there's no authenticator, a UnauthorizedException will be thrown.

SimpleHttpClient client = new SimpleHttpClient();

client.setAuthenticator((request) async{
  // Retrieve a new token
  String token = (await client.get("http://get.token")).body;

  // Set the header
  request.headers.set("Authorization", token);

You can specify how many times the authenticator should be called before the UnauthorizedException is thrown.

// Call the authenticator up to two times, throw `UnauthorizedException` if
// there's a `401` after those two calls.
SimpleHttpClient client = new SimpleHttpClient(maxAuthRetries: 2);

Reading the Response #

get(), post(), put(), and delete() return a Response object. This response contains the statusCode, body, and headers returned by the server.

Response response = await client.get("");

print(response.statusCode); // prints the code, e.g 200
print(response.body); // prints the body, e.g "Hello World"
print(response.headers); // prints the response headers

More examples #

Check the file test/simple_client_test.dart for detailed examples of all the features.

Changelog #

0.9.9 - Jan 19, 2020 #

  • Added addResponseIterceptor in order to allow for interception of the response for logging and analytics purposes
  • Renamed addInterceptor to addRequestInterceptor for clarity
  • Updated tests
  • Removed debug flag to promote the usage of ResponseInterceptor for logs

0.9.0 - Dec 30, 2018 #

First public version of the library

Added #

  • Support for interceptors
  • Support for authenticator
  • Basic requests returning the response
  • Basic GET, POST, PUT, and DELETE support
  • Timeout support
  • Redirection config support
  • TLS: config to accept any certificate
  • TLS: config to add a certificate to the trusted certs
  • Timeout, Unauthorized, and TLS Exceptions

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  simple_http_client: ^0.9.9

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:simple_http_client/simple_http_client.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jul 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Analysis suggestions

Package not compatible with runtime flutter-web on web

Because of the import of dart:io via the import chain package:simple_http_client/simple_http_client.dartdart:io

Package not compatible with runtime web

Because of the import of dart:io via the import chain package:simple_http_client/simple_http_client.dartdart:io

Health suggestions

Fix lib/simple_http_client.dart. (-10.44 points)

Analysis of lib/simple_http_client.dart reported 22 hints, including:

line 82 col 51: Unnecessary new keyword.

line 83 col 53: Unnecessary new keyword.

line 106 col 22: Use = to separate a named parameter from its default value.

line 107 col 19: Use = to separate a named parameter from its default value.

line 108 col 19: Use = to separate a named parameter from its default value.

Maintenance suggestions

The package description is too short. (-14 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and simple_http_client.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
mock_web_server >=4.0.0 <5.0.0
resource >=2.1.5 <3.0.0
test >=1.5.1 <2.0.0