无法通过本地PowerShell访问Azure OpenAI端点的问题求助
问题解决方法
PowerShell中执行curl命令失败的核心原因是:PowerShell里的curl是Invoke-WebRequest的别名,语法与原生curl(Azure CLI使用的)完全不同,以下是两种可行的解决方式:
方式一:适配PowerShell的Invoke-WebRequest语法
修正命令中的请求方法、请求体格式,确保符合PowerShell的语法要求:
# 定义请求头 $headers = @{ "Content-Type" = "application/json" "api-key" = "keygoeshere" } # 构造JSON格式的请求体 $body = @{ prompt = "Tell me a funny story" } | ConvertTo-Json # 发起POST请求 Invoke-WebRequest -Uri "https://aoai-try-mc.openai.azure.com/openai/deployments/try-davinci/completions?api-version=2022-12-01" -Headers $headers -Body $body -Method Post
也可以简化为单行命令:
curl "https://aoai-try-mc.openai.azure.com/openai/deployments/try-davinci/completions?api-version=2022-12-01" -Headers @{"Content-Type"="application/json"; "api-key"="keygoeshere"} -Body (@{prompt="Tell me a funny story"} | ConvertTo-Json) -Method Post
关键修正点:
- 显式指定
-Method Post:PowerShell的curl默认使用GET方法,而该API需要POST请求 - 将请求体转换为JSON字符串:直接传哈希表会被解析为表单数据,API无法识别,必须用
ConvertTo-Json转换 - URL用引号包裹:避免PowerShell错误解析
?开头的查询参数
方式二:调用原生curl程序
在PowerShell中直接使用curl.exe(而非PowerShell的别名),这样参数格式和Azure CLI完全一致:
curl.exe https://aoai-try-mc.openai.azure.com/openai/deployments/try-davinci/completions?api-version=2022-12-01 -H "Content-Type:application/json" -H "api-key:keygoeshere" -d '{"prompt":"Tell me a funny story"}'
这种方式无需修改参数,直接复用Azure CLI中的命令即可,Windows 10/11系统默认已包含原生curl。
内容的提问来源于stack exchange,提问作者Manu Chadha
相关产品推荐
相关产品推荐

