requests 3.1.0

  • Readme
  • Changelog
  • Installing
  • 94


a flutter library to make HTTP requests (inspired by python requests module). It comes with JSON support and a lightweight implementation to store cookies like a browser.

Cookies, huh? #

Server side cookies (via response header SET-COOKIE) are stored using the assistance of shared_preferences. Stored cookies will be send seamlessly on the next http requests you make to the same domain (simple implementation, similar to a web browser)

Install #

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

  requests: ^3.1.0

Usage #

Start by importing the library

import 'package:requests/requests.dart';

Let's make a simple HTTP request

var r = await Requests.get("");
String body = r.content();

the Response object #

just like in python's request module, the Response object has this functionallity

  • r.throwForStatus() - will throw an exception if the response statusCode is not a great success.
  • r.raiseForStatus() - same as throwForStatus
  • r.statusCode - the response status code
  • r.url - the url in the request
  • r.success - a boolean. true indicates that the request was a great success
  • r.hasError - a boolean. true indicates that the request was not a great success
  • r.bytes() - return the body in the respone as a list of bytes
  • r.content() - return the body in the respone as a string
  • r.json() - recodes the body in the respone and returns the result (dynamic type)

Optional Arguments #

  • json - a dynamic object that will be json encoded and then be set as the request's body
  • body - a raw string to be used as the request's body
  • bodyEncoding - default RequestBodyEncoding.FormURLEncoded. will set the content-type header
  • headers - Map<String, String> of custom client headers to add in the request
  • timeoutSeconds - default 10 seconds. after that period of time without server response an exception is thrown
  • persistCookies - default true. if should respect server's command to persist cookie
  • verify - default true. if the SSL verification enabled

💡 Only one optional argument can be used in a single request body or json

Class Methods #

  • .getHostname(url) - returns the hostname of the given url
  • .clearStoredCookies(hostname) - clears the stored cookies for the hostname
  • .setStoredCookies(hostname, Map<String, String>) - set the stored cookies for the hostname
  • .getStoredCookies(hostname) - returns a Map<String, String> of the stored cookies for the hostname

Examples #

HTTP post, body encoded as application/x-www-form-urlencoded, parse response as json

var r = await
  body: {
    "userId": 10,
    "id": 91,
    "title": "aut amet sed",
  bodyEncoding: RequestBodyEncoding.FormURLEncoded);

dynamic json = r.json();

HTTP delete

var r = await Requests.delete("");

Ignore SSL self-signed certificate

var r = await Requests.get('', verify: false);

Play with stored cookies

String url = "";
String hostname = Requests.getHostname(url);
await Requests.clearStoredCookies(hostname);
await Requests.setStoredCookies(hostname, {'session': 'bla'});
var cookies = await Requests.getStoredCookies(hostname);
expect(cookies.keys.length, 1);
await Requests.clearStoredCookies(hostname);
cookies = await Requests.getStoredCookies(hostname);
expect(cookies.keys.length, 0);

Business vector created by freepik -

1.0.0 #

  • Initial version

3.0.1 #

  • updated api with breaking changes

3.0.3 #

  • fix: parse json only if requested

3.0.4 #

  • fix: added body in patch and delete methods

3.0.5 #

  • fix: body in delete method

3.1.0 #

  • feature: added optional parameter port (also supporting inline url representation e.g. "")
  • feature: added queryParameters support in get requests
  • feature: created Github QA workflow to prevent pushes that breaks analyze or tests
  • enhancement: changed http method strings into enum to avoid errors
  • enhancement: improved type of arguments to prevent unexpected errors
  • enhancement: added more validations on tests
  • enhancement: executed flutter format to improve pub score
  • fix: cookie matching incorrect variable + case insensitive

Use this package as a library

1. Depend on it

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

  requests: ^3.1.0

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support 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:requests/requests.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Mar 31, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health issues and suggestions

Document public APIs. (-1 points)

45 out of 45 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/common.dart. (-1 points)

Analysis of lib/src/common.dart reported 2 hints:

line 54 col 15: Avoid using braces in interpolation when not needed.

line 54 col 20: Avoid using braces in interpolation when not needed.

Fix lib/src/requests.dart. (-0.50 points)

Analysis of lib/src/requests.dart reported 1 hint:

line 34 col 57: Avoid using braces in interpolation when not needed.

Maintenance suggestions

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

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
crypto ^2.0.6 2.1.4
flutter 0.0.0
hex ^0.1.2 0.1.2
http ^0.12.0 0.12.0+4
logging ^0.11.3+2 0.11.4
shared_preferences ^0.5.2 0.5.6+3
Transitive dependencies
async 2.4.1
charcode 1.1.3
collection 1.14.11 1.14.12
convert 2.1.1
flutter_web_plugins 0.0.0
http_parser 3.1.4
meta 1.1.8
path 1.6.4
shared_preferences_macos 0.0.1+6
shared_preferences_platform_interface 1.0.3
shared_preferences_web 0.1.2+4
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
pedantic ^1.0.0 1.9.0
test ^1.0.0