Giter Site home page Giter Site logo

requests's Introduction

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.3.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 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.headers - the response headers
  • 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

requests's People

Contributors

jossef avatar frederikpietzko avatar anirudh24seven avatar baskar007 avatar filledstacks avatar imdatsolak avatar juvs avatar lehoanglong avatar genesiscz avatar rafaelcmm avatar eggate avatar obuhovsergai avatar tlkops avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.