A composable, Future-based library for making HTTP requests.

Installing

Use pub to install this package. Add the following to your pubspec.yaml file.

dependencies:
  http: any

Then run pub install.

For more information, see the http package on pub.dartlang.org.

The easiest way to use this library is via the top-level functions. They allow you to make individual HTTP requests with minimal hassle:

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

var url = "http://example.com/whatsit/create";
http.post(url, fields: {"name": "doodle", "color": "blue"})
    .then((response) {
  print("Response status: ${response.statusCode}");
  print("Response body: ${response.body}");
});

http.read("http://example.com/foobar.txt").then(print);

If you're making multiple requests to the same server, you can keep open a persistent connection by using a Client rather than making one-off requests. If you do this, make sure to close the client when you're done:

var client = new http.Client();
client.post(
    "http://example.com/whatsit/create",
    fields: {"name": "doodle", "color": "blue"})
  .then((response) => client.get(response.bodyFields['uri']))
  .then((response) => print(response.body))
  .whenComplete(client.close);

You can also exert more fine-grained control over your requests and responses by creating Request or StreamedRequest objects yourself and passing them to Client.send.

This package is designed to be composable. This makes it easy for external libraries to work with one another to add behavior to it. Libraries wishing to add behavior should create a subclass of BaseClient that wraps another Client and adds the desired behavior:

class UserAgentClient extends http.BaseClient {
  final String userAgent;
  final http.Client _inner;

  UserAgentClient(this.userAgent, this._inner);

  Future<StreamedResponse> send(BaseRequest request) {
    request.headers[HttpHeaders.USER_AGENT] = userAgent;
    return _inner.send(request);
  }
}

Functions

delete(url, {Map<String, String> headers}) → Future<Response>

Sends an HTTP DELETE request with the given headers to the given URL, which can be a Uri or a String.

get(url, {Map<String, String> headers}) → Future<Response>

Sends an HTTP GET request with the given headers to the given URL, which can be a Uri or a String.

Sends an HTTP HEAD request with the given headers to the given URL, which can be a Uri or a String.

post(url, {Map<String, String> headers, Map<String, String> fields}) → Future<Response>

Sends an HTTP POST request with the given headers and fields to the given URL, which an be a Uri or a String. If any fields are specified, the content-type is automatically set to "application/x-www-form-urlencoded".

put(url, {Map<String, String> headers, Map<String, String> fields}) → Future<Response>

Sends an HTTP POST request with the given headers and fields to the given URL, which can be a Uri or a String. If any fields are specified, the content-type is automatically set to "application/x-www-form-urlencoded".

read(url, {Map<String, String> headers}) → Future<String>

Sends an HTTP GET request with the given headers to the given URL, which can be a Uri or a String, and returns a Future that completes to the body of the response as a String.

readBytes(url, {Map<String, String> headers}) → Future<Uint8List>

Sends an HTTP GET request with the given headers to the given URL, which can be a Uri or a String, and returns a Future that completes to the body of the response as a list of bytes.

Classes

BaseClient

The abstract base class for an HTTP client. This is a mixin-style class; subclasses only need to implement send and maybe close, and then they get various convenience methods for free.

BaseRequest

The base class for HTTP requests.

BaseResponse

The base class for HTTP responses.

ByteStream

A stream of chunks of bytes representing a single piece of data.

Client

The interface for HTTP clients that take care of maintaining persistent connections across multiple requests to the same server. If you only need to send a single request, it's usually easier to use head, get, post, put, or delete instead.

MultipartFile

A file to be uploaded as part of a MultipartRequest. This doesn't need to correspond to a physical file.

MultipartRequest

A multipart/form-data request. Such a request has both string fields, which function as normal form fields, and (potentially streamed) binary files.

Request

An HTTP request where the entire request body is known in advance.

Response

An HTTP response where the entire response body is known in advance.

StreamedRequest

An HTTP request where the request body is sent asynchronously after the connection has been established and the headers have been sent.

StreamedResponse

An HTTP response where the response body is received asynchronously after the headers have been received.