simple_http_client 0.9.0

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • new47

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 packages get

Alternatively, your editor might support pub get or flutter packages 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]
15
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
50
Overall:
Weighted score of the above. [more]
47
Learn more about scoring.

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

  • Dart: 2.1.0
  • pana: 0.12.10

Platforms

Detected platforms: Flutter, other

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

Maintenance issues and suggestions

Homepage URL does not exists. (-20 points)

We were unable to access https://git.starcarr.co/Dart/SimpleWebClient at the time of the analysis.

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use simple_http_client.dart.

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