http 0.11.3+17

http #

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

This package contains a set of high-level functions and classes that make it easy to consume HTTP resources. It's platform-independent, and can be used on both the command-line and the browser. Currently the global utility functions are unsupported on the browser; see "Using on the Browser" below.

Using #

The easiest way to use this library is via the top-level functions, although they currently only work on platforms where dart:io is available. 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, body: {"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",
    body: {"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['user-agent'] = userAgent;
    return _inner.send(request);
  }
}

Using on the Browser #

The HTTP library can be used on the browser via the BrowserClient class in package:http/browser_client.dart. This client translates requests into XMLHttpRequests. For example:

import 'dart:async';
import 'package:http/browser_client.dart';

main() async {
  var client = new BrowserClient();
  var url = '/whatsit/create';
  var response =
      await client.post(url, body: {'name': 'doodle', 'color': 'blue'});
  print('Response status: ${response.statusCode}');
  print('Response body: ${response.body}');
}

0.11.3+17 #

  • Use new Dart 2 constant names. This branch is only for allowing existing code to keep running under Dart 2.

0.11.3+16 #

  • Stop depending on the stack_trace package.

0.11.3+15 #

  • Declare support for async 2.0.0.

0.11.3+14 #

  • Remove single quote ("'" - ASCII 39) from boundary characters. Causes issues with Google Cloud Storage.

0.11.3+13 #

  • remove boundary characters that package:http_parser cannot parse.

0.11.3+12 #

  • Don't quote the boundary header for MultipartRequest. This is more compatible with server quirks.

0.11.3+11 #

  • Fix the SDK constraint to only include SDK versions that support importing dart:io everywhere.

0.11.3+10 #

  • Stop using dart:mirrors.

0.11.3+9 #

  • Remove an extra newline in multipart chunks.

0.11.3+8 #

  • Properly specify Content-Transfer-Encoding for multipart chunks.

0.11.3+7 #

  • Declare compatibility with http_parser 3.0.0.

0.11.3+6 #

  • Fix one more strong mode warning in http/testing.dart.

0.11.3+5 #

  • Fix some lingering strong mode warnings.

0.11.3+4 #

  • Fix all strong mode warnings.

0.11.3+3 #

  • Support http_parser 2.0.0.

0.11.3+2 #

  • Require Dart SDK >= 1.9.0

  • Eliminate many uses of Chain.track from the stack_trace package.

0.11.3+1 #

  • Support http_parser 1.0.0.

0.11.3 #

  • Add a Client.patch shortcut method and a matching top-level patch method.

0.11.2 #

  • Add a BrowserClient.withCredentials property.

0.11.1+3 #

  • Properly namespace an internal library name.

0.11.1+2 #

  • Widen the version constraint on unittest.

0.11.1+1 #

  • Widen the version constraint for stack_trace.

0.11.1 #

  • Expose the IOClient class which wraps a dart:io HttpClient.

0.11.0+1 #

  • Fix a bug in handling errors in decoding XMLHttpRequest responses for BrowserClient.

0.11.0 #

  • The package no longer depends on dart:io. The BrowserClient class in package:http/browser_client.dart can now be used to make requests on the browser.

  • Change MultipartFile.contentType from dart:io's ContentType type to http_parser's MediaType type.

  • Exceptions are now of type ClientException rather than dart:io's HttpException.

0.10.0 #

  • Make BaseRequest.contentLength and BaseResponse.contentLength use null to indicate an unknown content length rather than -1.

  • The contentLength parameter to new BaseResponse is now named rather than positional.

  • Make request headers case-insensitive.

  • Make MultipartRequest more closely adhere to browsers' encoding conventions.

Use this package as a library

1. Depend on it

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


dependencies:
  http: ^0.11.3+17

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:http/http.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
100
Health:
Code health derived from static analysis. [more]
92
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
68
Overall:
Weighted score of the above. [more]
91
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:http/http.dart.

Health suggestions

Fix lib/src/response.dart. (-2.96 points)

Analysis of lib/src/response.dart reported 6 hints, including:

line 34 col 35: Use = to separate a named parameter from its default value.

line 35 col 23: Use = to separate a named parameter from its default value.

line 36 col 33: Use = to separate a named parameter from its default value.

line 52 col 35: Use = to separate a named parameter from its default value.

line 53 col 23: Use = to separate a named parameter from its default value.

Fix lib/src/base_response.dart. (-1.49 points)

Analysis of lib/src/base_response.dart reported 3 hints:

line 43 col 20: Use = to separate a named parameter from its default value.

line 44 col 23: Use = to separate a named parameter from its default value.

line 45 col 33: Use = to separate a named parameter from its default value.

Fix lib/src/streamed_response.dart. (-1.49 points)

Analysis of lib/src/streamed_response.dart reported 3 hints:

line 26 col 35: Use = to separate a named parameter from its default value.

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

line 28 col 33: Use = to separate a named parameter from its default value.

Fix additional 14 files with analysis or formatting issues. (-2.50 points)

Additional issues in the following files:

  • lib/browser_client.dart (2 hints)
  • lib/src/base_client.dart (2 hints)
  • lib/src/multipart_request.dart (1 hint)
  • lib/http.dart (Run dartfmt to format lib/http.dart.)
  • lib/src/base_request.dart (Run dartfmt to format lib/src/base_request.dart.)
  • lib/src/boundary_characters.dart (Run dartfmt to format lib/src/boundary_characters.dart.)
  • lib/src/byte_stream.dart (Run dartfmt to format lib/src/byte_stream.dart.)
  • lib/src/client.dart (Run dartfmt to format lib/src/client.dart.)
  • lib/src/io_client.dart (Run dartfmt to format lib/src/io_client.dart.)
  • lib/src/mock_client.dart (Run dartfmt to format lib/src/mock_client.dart.)
  • lib/src/multipart_file.dart (Run dartfmt to format lib/src/multipart_file.dart.)
  • lib/src/request.dart (Run dartfmt to format lib/src/request.dart.)
  • lib/src/streamed_request.dart (Run dartfmt to format lib/src/streamed_request.dart.)
  • lib/src/utils.dart (Run dartfmt to format lib/src/utils.dart.)

Maintenance issues and suggestions

Use analysis_options.yaml. (-10 points)

Change the name of your package's .analysis_options file to analysis_options.yaml.

Read more about the setup of analysis-options.yaml.

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 http.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Package is getting outdated. (-8.49 points)

The package was last published 56 weeks ago.

The package description is too short. (-4 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.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.61.0 <3.0.0
async >=1.10.0 <3.0.0 2.3.0
collection ^1.5.0 1.14.11
http_parser >=0.0.1 <4.0.0 3.1.3
path >=0.9.0 <2.0.0 1.6.4
Transitive dependencies
charcode 1.1.2
meta 1.1.7
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
unittest >=0.9.0 <0.12.0