simple_http_client 0.9.0

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 64

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.

GET #

To perform a GET request you can simply do:

SimpleHttpClient client = new SimpleHttpClient();

Response response = await client.get("http://example.com");

POST #

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

SimpleHttpClient client = new SimpleHttpClient();
Response response =  await client.post("http://exa.co", "{\"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("http://exa.co",
    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("http://exa.co");

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);
client.setAuthenticator(...);

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("http://exa.co");

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.0 - Dec 30, 2018 #

First public version of the lib

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:


dependencies:
  simple_http_client: ^0.9.0

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';
  
Version Uploaded Documentation Archive
0.9.0 Dec 30, 2018 Go to the documentation of simple_http_client 0.9.0 Download simple_http_client 0.9.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
41
Health:
Code health derived from static analysis. [more]
96
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
76
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Jun 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18

Platforms

Detected platforms: Flutter, other

Primary library: package:simple_http_client/simple_http_client.dart with components: io.

Health suggestions

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

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

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

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

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

line 102 col 27: Use = to separate a named parameter from its default value.

line 103 col 24: 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/README.md.

For more information see the pub package layout conventions.

Dependencies

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