Skip to content

接口调试命令

命令行发请求最常用的工具是 curl,但不同终端(Git Bash / Linux、Windows CMD、PowerShell)的续行符和引号处理不同,下面按终端分别给出等价示例。

通用参数:-X 方法、-H 请求头、-d 请求体、-v 打印请求/响应头、-i 只打印响应头。

示例:POST JSON(以 http://ip/tongyi/fetch 为例)

1. Git Bash / Linux(bash)— 最推荐

bash
curl -X POST http://ip/tongyi/fetch \
  -H "Content-Type: application/json" \
  -d '{"prompt":"你好"}'

3. PowerShell(最常见常用:curl.exe + 反引号续行)

PowerShell 里 curlInvoke-WebRequest 的别名,建议用 curl.exe 直接调原生 curl;多行用反引号 ` 续行。语法与第 1 节(Git Bash)基本一致,只把续行符 \ 换成 `,日常手动敲命令首选这个。

powershell
curl.exe -X POST http://ip/tongyi/fetch `
  -H "Content-Type: application/json" `
  -d '{"prompt":"你好"}'

4. PowerShell 原生(Invoke-RestMethod):写脚本 / 需要对象返回时用

完全不用 curl,用 PowerShell 自带命令;请求体写成哈希表再 ConvertTo-Json。返回结果是 PowerShell 对象(可直接 .属性 取值),适合写在 .ps1 脚本里、或要对响应做进一步处理时使用,手动发单次请求不划算。

powershell
$body = @{ prompt = "你好" } | ConvertTo-Json
Invoke-RestMethod -Uri "http://ip/tongyi/fetch" `
  -Method Post `
  -ContentType "application/json" `
  -Body $body

2. Windows CMD — 不推荐

CMD 没有 \ 续行,用 ^ 续行;且双引号内不能裸用双引号,JSON 里的双引号要写成 \"

bat
curl -X POST http://ip/tongyi/fetch ^
  -H "Content-Type: application/json" ^
  -d "{\"prompt\":\"你好\"}"

GET 带查询参数

bash
# bash / git bash
curl "http://ip/api/user?id=1&name=test"
bat
:: CMD(& 在 cmd 里是命令连接符,需转义为 ^&)
curl "http://ip/api/user?id=1^&name=test"
powershell
# PowerShell(& 是管道绑定符,需引号包裹或转义 `&)
curl.exe "http://ip/api/user?id=1&name=test"

带鉴权头(Bearer Token)

bash
# bash / git bash / PowerShell(curl.exe)
curl -X GET http://ip/api/info \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json"
bat
:: CMD
curl -X GET http://ip/api/info ^
  -H "Authorization: Bearer <token>" ^
  -H "Content-Type: application/json"

终端差异速记

终端续行符单引号JSON 双引号
Git Bash / Linux\✅ 支持原样写
Windows CMD^❌ 不支持写成 \"
PowerShell`(反引号)✅ 支持原样写(用 curl.exe 时)

推荐与主流占比(按使用频率)

排名方案推荐度主流占比(估算)说明
1① Git Bash / Linux(curl)⭐⭐⭐ 最推荐~45%跨平台语法一致,教程 / CI / Mac·Linux 通用
2③ PowerShell(curl.exe)⭐⭐⭐ 推荐~35%Windows 默认终端,语法同 ①,只换续行符
3④ PowerShell 原生(Invoke-RestMethod)⭐⭐ 脚本专用~5%仅写 .ps1 脚本 / 需要对象返回时用
4② Windows CMD⭐ 不推荐~15%续行 / 引号最坑,尽量避开

结论:首选 ①(Git Bash)或 ③(PowerShell + curl.exe),两者语法几乎一样;日常 80% 场景落在这两种。② CMD 最折腾、能不用就不用;④ 是 PowerShell 脚本里的专用写法。 要点:PowerShell 优先用 curl.exe 而非 curl(后者是 Invoke-WebRequest 别名,参数不同)。

© 2026 开发速查