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

使用PowerShell调用Cognos Analytics REST API遇415错误求助

Cognos Analytics 11.2.4 REST API 登录会话创建报415错误的解决指引

问题描述

我在使用Cognos Analytics 11.2.4,尝试编写PowerShell脚本通过REST API管理系统,第一步创建新会话(登录)始终失败,每次都会报错:
Invoke-RestMethod : The remote server returned an error: (415) Unsupported Media Type.

我参考了IBM官方REST API文档、服务器本地的API文档、PowerShell的Invoke-RestMethod文档,以及相关的415错误问题资料,但服务器本地API文档未列出415作为可能响应,现有文档也没说明错误原因。

以下是我尝试过的代码(包含已注释的之前的尝试):

# 文档地址
# http://<cognos_analytics_server>:<port>/api/api-docs

$serverName = "<cognos_analytics_server>"
$port = "<port>"
$userNamespace = "<external_directory_namespace>"
$userName = Read-Host "User Name:  "

$pwd = Read-Host "Password" -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)
$userPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
# 释放非托管内存
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)

# 尝试1
#$p = @()
#$p += @{
#      name = "CAMNamespace"
#      value = $userNamespace
#    }
#$p += @{
#      name = "CAMUsername"
#      value = $userName
#    }
#$p += @{
#      name = "CAMPassword"
#      value = $userPassword
#    }
#
#$body = @{
#  parameters = $p
#}

# 尝试2
#$body = '{
#  "parameters": [
#    {
#      "name": "CAMNamespace",
#      "value": "' + $userNamespace + '"
#    },
#    {
#      "name": "CAMUsername",
#      "value": "' + $userName + '"
#    },
#    {
#      "name": "CAMPassword",
#      "value": "' + $userPassword + '"
#    }
#  ]
#}'

# 尝试3
#$body = '{"parameters": [{"name": "CAMNamespace","value": "' + $userNamespace + '"},{"name": "CAMUsername","value": "' + $userName + '"},{"name": "CAMPassword","value": "' + $userPassword + '"}]}'

# 尝试4
#$body = "{`"parameters`": [{`"name`": `"CAMNamespace`",`"value`": `"$userNamespace`"},{`"name`": `"CAMUsername`",`"value`": `"$userName`"},{`"name`": `"CAMPassword`",`"value`": `"$userPassword`}]}"

# 尝试5
$body = "{
  `"parameters`": [
    {
      `"name`": `"CAMNamespace`",
      `"value`": `"$userNamespace`"
    },
    {
      `"name`": `"CAMUsername`",
      `"value`": `"$userName`"
    },
    {
      `"name`": `"CAMPassword`",
      `"value`": `"$userPassword`"
    }
  ]
}"


$uri = "$protocol`://$serverName`:$port/api/v1/session"

# 尝试1
#Invoke-RestMethod -Uri $uri -Method Put -Body $body

# 后续尝试
Invoke-RestMethod -contentType "text/plain" -Uri $uri -Method Put -Body $body

解决指引

1. 核心问题:Content-Type 配置错误

415错误的本质是服务器不接受你发送的请求媒体类型。Cognos Analytics REST API要求登录请求的Content-Type必须为application/json,但你最后一次尝试中设置的是text/plain,这直接触发了错误。

2. 避免手动拼接JSON字符串

手动拼接JSON容易出现转义错误、格式异常(比如特殊字符、空格处理),PowerShell提供的ConvertTo-Json命令可以生成标准、可靠的JSON格式。

修正后的代码示例

$serverName = "<cognos_analytics_server>"
$port = "<port>"
$protocol = "https" # 根据实际环境改为http或https
$userNamespace = "<external_directory_namespace>"
$userName = Read-Host "User Name:  "

$pwd = Read-Host "Password" -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)
$userPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)

# 使用PowerShell对象生成标准JSON
$bodyObject = @{
    parameters = @(
        @{ name = "CAMNamespace"; value = $userNamespace },
        @{ name = "CAMUsername"; value = $userName },
        @{ name = "CAMPassword"; value = $userPassword }
    )
}
$body = $bodyObject | ConvertTo-Json -Depth 3

$uri = "$protocol://$serverName`:$port/api/v1/session"

# 关键:设置正确的Content-Type,并保留会话cookie(后续API调用需要)
$sessionResponse = Invoke-RestMethod -Uri $uri -Method Put -Body $body -ContentType "application/json" -SessionVariable "cognosSession"

# 验证登录结果
Write-Host "登录成功,会话ID: $($sessionResponse.sessionId)"

其他检查要点

  • 协议与端口:确认$protocol是http还是https,Cognos默认9300端口通常使用https,避免协议不匹配。
  • 命名空间准确性:$userNamespace必须是Cognos中配置的外部目录命名空间的准确名称(区分大小写)。
  • 账号权限:确保使用的账号具备通过REST API访问Cognos的权限。
  • SSL证书信任:如果使用https且服务器证书未被信任,可添加-SkipCertificateCheck参数(仅测试环境使用,生产环境建议导入信任证书)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 21:34:54