|
48 | 48 | | `pm.setGlobalVariable(key, value)` | 设置全局变量(实际存储在环境变量中) | `pm.setGlobalVariable('baseUrl', 'https://api.com')` | |
49 | 49 | | `pm.getGlobalVariable(key)` | 获取全局变量(实际从环境变量读取) | `pm.getGlobalVariable('baseUrl')` | |
50 | 50 | | `pm.getResponseCookie(name)` | 获取响应中的 Cookie | `pm.getResponseCookie('sessionId')` | |
| 51 | +| `pm.sendRequest(options, callback)` | 在脚本中发送额外 HTTP 请求 | `pm.sendRequest({ url: 'https://api.com' }, (err, resp) => {})` | |
51 | 52 |
|
52 | 53 | ### 外部数据源对象 |
53 | 54 |
|
|
58 | 59 | | `pm.es` / `pm.elasticsearch` | Elasticsearch 请求与查询 | `request(options)`、`query(options)` | |
59 | 60 | | `pm.influx` / `pm.influxdb` | InfluxDB Flux / InfluxQL 查询与写入 | `query(options)`、`write(options)`、`request(options)` | |
60 | 61 |
|
| 62 | +### pm.sendRequest() |
| 63 | + |
| 64 | +`pm.sendRequest()` 已支持在脚本中主动发起额外 HTTP 请求,适用于前置脚本里刷新 token、拉取签名、预热上下文数据等场景。 |
| 65 | + |
| 66 | +回调函数签名为 `(err, response)`: |
| 67 | + |
| 68 | +- `err`:请求失败时返回错误对象,包含 `message`、`name` |
| 69 | +- `response`:成功时返回响应包装对象,可使用 `response.code`、`response.status`、`response.headers.get()`、`response.text()`、`response.json()` |
| 70 | + |
| 71 | +支持的请求格式: |
| 72 | + |
| 73 | +- 直接传 URL 字符串,默认 `GET` |
| 74 | +- 传对象:`url`、`method`、`header`、`body` |
| 75 | +- `body.mode` 当前支持 `raw`、`formdata`、`urlencoded` |
| 76 | + |
| 77 | +示例: |
| 78 | + |
| 79 | +```javascript |
| 80 | +pm.sendRequest({ |
| 81 | + url: 'https://httpbin.org/get?source=easy-postman&from=pre-script', |
| 82 | + method: 'GET', |
| 83 | + header: { |
| 84 | + 'X-Debug-Token': pm.environment.get('debugToken') || 'demo-token' |
| 85 | + } |
| 86 | +}, function (err, response) { |
| 87 | + if (err) { |
| 88 | + console.error('请求失败:', err.message); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const data = response.json(); |
| 93 | + pm.environment.set('lastHttpbinUrl', data.url || ''); |
| 94 | + pm.environment.set('lastHttpbinSource', data.args?.source || ''); |
| 95 | + console.log('httpbin args:', data.args); |
| 96 | +}); |
| 97 | +``` |
| 98 | +
|
61 | 99 | --- |
62 | 100 |
|
63 | 101 | ## pm.environment - 环境变量 |
@@ -2244,8 +2282,35 @@ const currentTime = Date.now(); |
2244 | 2282 | // 检查 token 是否过期(提前5分钟刷新) |
2245 | 2283 | if (!tokenExpireTime || currentTime > (parseInt(tokenExpireTime) - 300000)) { |
2246 | 2284 | console.log('Token 即将过期或已过期,需要刷新'); |
2247 | | - // 在实际环境中,这里应该触发刷新 token 的逻辑 |
2248 | | - // 由于不支持 pm.sendRequest,建议在测试流程中手动添加刷新 token 的请求 |
| 2285 | + pm.sendRequest({ |
| 2286 | + url: pm.environment.get('authUrl'), |
| 2287 | + method: 'POST', |
| 2288 | + header: { |
| 2289 | + 'Content-Type': 'application/json' |
| 2290 | + }, |
| 2291 | + body: { |
| 2292 | + mode: 'raw', |
| 2293 | + raw: JSON.stringify({ |
| 2294 | + refreshToken: pm.environment.get('refreshToken') |
| 2295 | + }) |
| 2296 | + } |
| 2297 | + }, function (err, response) { |
| 2298 | + if (err) { |
| 2299 | + console.error('刷新 token 失败:', err.message); |
| 2300 | + return; |
| 2301 | + } |
| 2302 | +
|
| 2303 | + const data = response.json(); |
| 2304 | + const token = data.accessToken || data.token; |
| 2305 | + if (token) { |
| 2306 | + pm.environment.set('authToken', token); |
| 2307 | + pm.request.headers.upsert({ |
| 2308 | + key: 'Authorization', |
| 2309 | + value: 'Bearer ' + token |
| 2310 | + }); |
| 2311 | + console.log('✓ Token 刷新成功,已更新认证头'); |
| 2312 | + } |
| 2313 | + }); |
2249 | 2314 | } else { |
2250 | 2315 | const token = pm.environment.get('authToken'); |
2251 | 2316 | if (token) { |
@@ -2981,7 +3046,6 @@ pm.environment.set('performanceStats', JSON.stringify(stats)); |
2981 | 3046 | - 库代码会被缓存,重复加载不会影响性能 |
2982 | 3047 |
|
2983 | 3048 | 8. **不支持的功能** |
2984 | | - - ❌ `pm.sendRequest()` - 不支持在脚本中发送 HTTP 请求 |
2985 | 3049 | - ❌ `pm.iterationData` - 不支持迭代数据(但支持 CSV 数据驱动) |
2986 | 3050 | - ❌ `pm.info` - 不支持请求元信息访问 |
2987 | 3051 | - ❌ 完整的 Chai.js 断言库 |
|
0 commit comments