Files
2026-03-26 20:22:34 +08:00

61 lines
2.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
name: "ulthon-tools-http-call"
description: "命令行调用后台控制器/URL 进行联调与功能验证(含默认 super token 模拟登录)。"
---
# tools:http:call命令行 HTTP 调用工具)
## 何时使用
- 需要在命令行快速验证某个后台页面/接口是否正常返回。
- 需要绕过浏览器环境,在 CLI 中模拟已登录管理员请求。
- 需要用 `--app/--controller/--action` 直接拼控制器路径调用。
## 基本用法
### 1) 直接按 URL 调用
```bash
php think tools:http:call --url="/admin/system.admin/index" --method=GET
```
### 2) 按控制器参数调用
```bash
php think tools:http:call --app=admin --controller=system.admin --action=index --method=GET
```
## 请求参数
- `--url`:请求路径(支持以 `/` 开头的相对路径)。
- `--method`GET/POST/PUT/PATCH/DELETE。
- `--data`JSON 字符串,请求会自动带 `Content-Type: application/json`
- `--body`:原始请求体字符串。
- `--headers`JSON 字符串的请求头对象(例如 `{ "Accept":"application/json" }`)。
- `--app --controller --action`:框架特性参数,用于拼控制器路径。
## super token默认模拟登录
### 机制说明
- `tools:http:call` 默认会为每次请求生成一个新的 token并写入 `Cache::store('login')`
- 请求会自动携带 `Authorization: Bearer <token>`
- 服务端会按既有 Bearer token 机制从 `Cache::store('login')` 读取登录态,因此只要命令行与服务端使用同一套缓存即可生效。
### 指定模拟账号
```bash
php think tools:http:call --url="/admin/system.admin/index" --user-id=2
```
### 关闭 super token回到未登录行为
```bash
php think tools:http:call --url="/admin/system.admin/index" --super-token=false
```
## 常见现象与排查
- 仍提示“请先登录后台”:通常是命令行与服务端的缓存不共享,或服务端未运行/未走到同一环境配置。
- 返回“无权限访问”:说明已经是登录态,但该账号对当前节点没有权限;可以换 `--user-id` 或调整权限节点。