simple_http_client 0.9.9 copy "simple_http_client: ^0.9.9" to clipboard
simple_http_client: ^0.9.9 copied to clipboard

Simple HTTP client for Flutter and Dart server

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.

0
likes
20
pub points
1%
popularity

Publisher

unverified uploader

Simple HTTP client for Flutter and Dart server

Homepage

License

Apache-2.0 (LICENSE)

More

Packages that depend on simple_http_client