You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Powershell调用Cisco Firepower REST API获取令牌返回500错误咨询

注:以下示例中的IP地址、账号名、账号密码均已替换为X。

提问内容

我正尝试管理Cisco Firepower机箱设备的API,首个请求是获取密码令牌以用于后续REST API调用。已知使用设备自带测试页面直接提交JSON请求可正常运行,当前需要从Windows Server 2019服务器的另一台机器上,通过PowerShell发起该请求。

可用参考请求

  • 可正常运行的JSON请求体:
{
    "grant_type": "password",
    "username": "xxx",
    "password": "xxx"
}
  • 设备提供的CURL参考调用代码,提交后可正常返回200响应,包含后续请求所需的访问令牌:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
 "grant_type": "password", \ 
 "username": "xxx", \ 
 "password": "xxx" \ 
 }' 'https://x.x.x.x/api/fdm/v5/fdm/token'

我编写的PowerShell代码

$param = '{"grant_type":"password","username":"xxx",password":"xxx"}'
$json = $param | ConvertTo-Json
Invoke-RestMethod 'https://x.x.x.x/api/fdm/v5/fdm/token' -Method Post -Body $json -ContentType 'application/json'

报错信息

运行上述代码后,收到设备返回的错误响应,无法定位具体不被认可的请求内容。

完整报错

Invoke-RestMethod : HTTP Status 500 – Internal Server Error
H1 
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} 
H2 
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} 
H3 
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} 
BODY 
{font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} 
B 
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} 
P 
{font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}
A {color : black;}
A.name {color : black;}
.line {height: 1px; background-color: #525D76; border: none;}
HTTP Status 500 – Internal Server Error
At line:3 char:1
+ Invoke-RestMethod 'https://x.x.x.x/api/fdm/v5/fdm/token' -Method P ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

简化错误详情

Exception details: 
    Message: The remote server returned an error: (500) Internal Server Error.
    Status code: InternalServerError
    Status description: Internal Server Error
    Response: 

排查现状

已在Firepower设备上未查询到任何API请求或错误相关日志,需要定位当前PowerShell代码的问题。


解答

你的代码触发500错误的核心原因有3个,按优先级排序如下:

  1. 请求体JSON二次编码:你已经手动把$param写成了合法JSON字符串,又额外执行了ConvertTo-Json,这会把整个JSON字符串当成普通文本再次转义,设备收到的请求体不是合法的JSON结构,自然解析失败返回500。
  2. JSON格式笔误:原$param字符串里password字段的前置双引号缺失,写成了,password":"xxx",拼接出来的JSON本身就是非法的。
  3. 请求头不匹配:参考CURL请求里明确带了Accept: application/json头,你的PowerShell代码没有加该头,部分设备的API会校验必填请求头,不符合要求就会返回错误。

可正常运行的修正代码

# 1. 强制启用TLS 1.2,适配Server 2019默认未启用高版本加密的问题,避免和Firepower的TLS协商失败
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# 2. 直接构造PowerShell对象再转JSON,避免手动拼接JSON的笔误
$body = @{
    grant_type = "password"
    username = "xxx"
    password = "xxx"
} | ConvertTo-Json

# 3. 完整对齐CURL请求的参数
$requestParams = @{
    Uri = "https://x.x.x.x/api/fdm/v5/fdm/token"
    Method = "Post"
    Body = $body
    ContentType = "application/json"
    Headers = @{
        Accept = "application/json"
    }
}

Invoke-RestMethod @requestParams

如果还是报错,可以先输出$body的内容,确认和参考的JSON请求体完全一致后再发起请求。


内容的提问来源于stack exchange,提问作者CDK19

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.30 11:54:04