requests 3.0.3

  • Readme
  • Changelog
  • Installing
  • 91

banner-01

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:

dependencies:
  requests: ^3.0.3

Usage #

Start by importing the library

import 'package:requests/requests.dart';

Let's make a simple HTTP request

var r = await Requests.get("https://google.com");
r.raiseForStatus();
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 Requests.post(
  "https://reqres.in/api/users",
  body: {
    "userId": 10,
    "id": 91,
    "title": "aut amet sed",
  },
  bodyEncoding: RequestBodyEncoding.FormURLEncoded);

r.raiseForStatus();
dynamic json = r.json();
print(json['id']);

HTTP delete

var r = await Requests.delete("https://reqres.in/api/users/10");
r.raiseForStatus();

Ignore SSL self-signed certificate

var r = await Requests.get('https://expired.badssl.com/', verify: false);
r.raiseForStatus();

Play with stored cookies

String url = "https://reqres.in/api/users/10";
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 - www.freepik.com

1.0.0 #

  • Initial version

3.0.1 #

  • updated api with breaking changes

3.0.3 #

  • fix: parse json only if requested

Use this package as a library

1. Depend on it

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


dependencies:
  requests: ^3.0.3

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

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

  • Dart: 2.6.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-1 points)

43 out of 43 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 31 col 73: Avoid using braces in interpolation when not needed.

Format lib/requests.dart.

Run flutter format to format lib/requests.dart.

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/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
crypto ^2.0.6 2.1.3
flutter 0.0.0
hex ^0.1.2 0.1.2
http ^0.12.0 0.12.0+2
logging ^0.11.3+2 0.11.3+2
shared_preferences ^0.5.2 0.5.4+5
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.11 1.14.12
convert 2.1.1
http_parser 3.1.3
meta 1.1.7 1.1.8
path 1.6.4
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
pedantic ^1.0.0 1.8.0+1
test ^1.0.0