jossef / requests

MIT License
140 stars 54 forks source link

banner-01

Version License

a dart 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 quiver.cache. 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: ^4.8.0-alpha.0

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 functionality

Optional Arguments

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

Class Methods

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://example.com";
await Requests.clearStoredCookies(url);

// Set cookies using [CookieJar.parseCookiesString]
var cookies = CookieJar.parseCookiesString("name=value");
await Requests.setStoredCookies(url, cookies);

// Add single cookie using [CookieJar.parseCookiesString]
var cookieJar = await Requests.getStoredCookies(url);
cookieJar["name"] = Cookie("name", "value");
await Requests.setStoredCookies(url, cookieJar);

// Add a single cookie using [Requests.addCookie]
// Same as the above one but without exposing `cookies.dart`
Requests.addCookie(url, "name", "value");

More examples can be found in example/.

Business vector created by freepik - www.freepik.com