搜索插件、代码仓与 APP 分类

插件详情v4.7.0

requests

一个用于 HTTP 请求和存储 Cookie 的 Flutter 库

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
3410
喜欢数
104
Pub 分数
130

安装插件

flutter pub add requests

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

requests 包是一个专为 Flutter 设计的库,旨在简化 HTTP 请求并在跨平台环境中管理存储的 cookie。它提供了一个直观的 API,用于执行 GET、POST 及其他 HTTP 方法,并内置了对请求头、查询参数和响应处理的支持。该库包含自动的 cookie 持久化功能,可在类似 Web 的应用中实现无缝会话管理。兼容 Android、iOS、Web、macOS、Windows 和 Linux,非常适合需要可靠网络通信和有状态交互的应用程序。其轻量级设计和简单的集成方式,使开发者能够高效地进行 HTTP 操作,而无需编写冗余代码。

适合场景

  • 进行身份验证的 API 调用
  • 处理会话 Cookie
  • 简化 REST API 集成
  • 管理请求头和负载
  • 测试网络端点
  • 构建类似网页的应用体验

主要特点

  • 自动存储和检索 Cookie
  • 支持所有主要的 HTTP 方法
  • 跨平台兼容性
  • 易于使用的同步和异步 API
  • 内置的响应解析工具
  • 可配置的请求超时

适合对象

  • 开发具有持久会话的应用程序的开发者
  • 需要强大 HTTP 客户端功能的应用程序
  • 需要一致的 cookie 管理的项目
  • 希望HTTP请求设置最少的团队
  • 网络功能的原型设计或快速开发

使用建议

  • 需要仔细处理异步操作
  • 可能需要额外的错误处理以用于生产环境
  • Cookie 存储行为可能因平台而异
  • 不适合高性能、低延迟的场景
  • 与功能齐全的客户端相比,高级功能有限
  • 依赖外部GitHub仓库进行更新

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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

  • 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 = 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

所属分类

依赖

依赖清单 4 项

源码仓库

使用此插件的项目

暂无相关数据。