示例可能把真实 Bearer 令牌发送给第三方演示服务器
原文依据:2 处示例在固定的 `jsonplaceholder.typicode.com` 请求中放置了 Authorization 头,并使用 `your_token_here` 提示替换令牌。若用户直接替换占位符却未同时改为自己信任的 API,真实凭据会被发送给该第三方。
第三方可能获得令牌,并在令牌仍有效且权限允许时访问相应账户或 API。令牌也会以明文形式留在应用源代码和编译产物中。
这是示例代码,但它把 Authorization 头与固定的第三方演示域名放在同一个真实请求中。占位符本身不是秘密;只有用户用真实令牌替换它、却没有把 URL 改为令牌所属且受信任的 API 时,令牌才会发送给 jsonplaceholder.typicode.com。用户可要求作者删除演示请求中的认证头,或明确要求令牌和 API 主机必须匹配。
* **URIs:** Always parse URL strings using `Uri.parse('your_url')`.* **Headers:** Inject authorization and content-type headers via the `headers` parameter map. Use `HttpHeaders.authorizationHeader` for auth tokens.* **Payloads:** For POST and PUT requests, encode the body using `jsonEncode()` from `dart:convert`.* **Status Validation:** Evaluate `response.statusCode`. Treat `200 OK` (GET/PUT/DELETE) and `201 CREATED` (POST) as success. 查看另外 1 个位置
// 2. Network execution with background parsingFuture<List<Photo>> fetchPhotos() async { final response = await http.get( Uri.parse('https://jsonplaceholder.typicode.com/photos'), headers: { HttpHeaders.authorizationHeader: 'Bearer your_token_here', HttpHeaders.acceptHeader: 'application/json', }, );