Package selection guidance compiled from public package information, community data, and project usage.
The requests package is a Flutter library designed to simplify HTTP requests and manage stored cookies across platforms. It provides an intuitive API for making GET, POST, and other HTTP methods with built-in support for headers, query parameters, and response handling. The library includes automatic cookie persistence, enabling seamless session management in web-like applications. Compatible with Android, iOS, Web, macOS, Windows, and Linux, it's ideal for apps requiring reliable network communication and stateful interactions. Its lightweight design and straightforward integration make it accessible for developers seeking efficient HTTP operations without boilerplate code.
Use cases
Making authenticated API calls
Handling session cookies
Simplifying REST API integrations
Managing request headers and payloads
Testing network endpoints
Building web-like app experiences
Key features
Automatic cookie storage and retrieval
Support for all major HTTP methods
Cross-platform compatibility
Easy-to-use synchronous and asynchronous APIs
Built-in response parsing utilities
Configurable request timeouts
Suitable for
Developers building apps with persistent sessions
Apps requiring robust HTTP client functionality
Projects needing consistent cookie management
Teams wanting minimal setup for HTTP requests
Prototyping or rapid development of networked features
Considerations
Requires careful handling of async operations
May need additional error handling for production use
Cookie storage behavior may vary by platform
Not suitable for high-performance, low-latency scenarios
Limited advanced features compared to full-featured clients
Depends on external GitHub repository for updates
Original README
Original README snapshot from the linked source repository. Visit the repository for the latest content.
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 = awaitRequests.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 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 response as a list of bytes
r.content() - return the body in the response as a string
r.json() - recodes the body in the response 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
withCredentials - default false. for dart web to handle cookies, authorization headers, or TLS client certificates
Note:
Only one optional argument can be used in a single request body or json
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 = awaitRequests.delete('https://reqres.in/api/users/10');
r.raiseForStatus();
Ignore SSL self-signed certificate
var r = awaitRequests.get('https://expired.badssl.com/', verify: false);
r.raiseForStatus();
Play with stored cookies
String url = "https://example.com";
awaitRequests.clearStoredCookies(url);
// Set cookies using [CookieJar.parseCookiesString]var cookies = CookieJar.parseCookiesString("name=value");
awaitRequests.setStoredCookies(url, cookies);
// Add single cookie using [CookieJar.parseCookiesString]var cookieJar = awaitRequests.getStoredCookies(url);
cookieJar["name"] = Cookie("name", "value");
awaitRequests.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");