搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。