requests_plus 4.8.5 requests_plus: ^4.8.5 copied to clipboard
a flutter library that helps with http requests and stored cookies
🚀 requests-plus #
A powerful Dart library for making HTTP requests, based on the popular requests module from Python. This is a fork of the original "requests" project with additional features and enhancements.
🌟 New Features #
- Form Data Support: Send data in the form of
multipart/form-data
using theRequestBodyEncoding.FormData
option in thebodyEncoding
parameter. - HTTP Authentification support: Easily send HTTP authentification headers using the
userName
andpassword
parameters. (Basic Authentification only) - CORS Proxy: Easily bypass CORS restrictions by utilizing the dart_cors-proxy proxy. Simply specify the
corsProxyUrl
parameter with the URL of the proxy.
Note: This fork builds upon the functionality of the original "requests" library, adding these new features to empower your HTTP requests.
🍪 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).
✨ Installation #
Add this to your package's pubspec.yaml file:
dependencies:
requests: ^4.8.2
Usage #
Start by importing the library
import 'package:requests/requests_plus.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
r.throwForStatus()
- will throw an exception if the responsestatusCode
is not a great success.r.raiseForStatus()
- same asthrowForStatus
r.statusCode
- the response status coder.url
- the url in the requestr.headers
- the response headersr.success
- a boolean.true
indicates that the request was a great successr.hasError
- a boolean.true
indicates that the request was not a great successr.bytes()
- return the body in the response as a list of bytesr.content()
- return the body in the response as a stringr.json()
- recodes the body in the response and returns the result (dynamic type)
Optional Arguments #
json
- adynamic
object that will be json encoded and then be set as the request's bodybody
- a raw string to be used as the request's bodybodyEncoding
- defaultRequestBodyEncoding.FormURLEncoded
. will set thecontent-type
headerheaders
-Map<String, String>
of custom client headers to add in the request (will override all default headers)timeoutSeconds
- default10
seconds. after that period of time without server response an exception is thrownpersistCookies
- defaulttrue
. if should respect server's command to persist cookieverify
- defaulttrue
. if the SSL verification enabledwithCredentials
- defaultfalse
. for dart web to handle cookies, authorization headers, or TLS client certificatesuserName
- defaultnull
. for HTTP basic authentificationpassword
- defaultnull
. for HTTP basic authentification
Note: Only one optional argument can be used in a single request
body
orjson
Class Methods #
.clearStoredCookies(url)
- clears the stored cookies for the url.setStoredCookies(url, CookieJar)
- set the stored cookies for the url.getStoredCookies(url)
- returns a CookieJar of the stored cookies for the url.addCookie(url, name, value)
- add a Cookie to the CookieJar associated to the url
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/.