Using Cognos Analytics 11.2.4
Trying to write a PowerShell script to administer Cognos Analytics through the Cognos Analytics REST API. My first attempt is to create a new session (login). I'm getting nowhere so far.
Following documentation and help found at:
IBM Cognos Analytics REST API (and related)
https://MyServerName:9300/api/api-docs/
PowerShell Invoke-RestMethod
Unsupported Media Type when updating values using the TeamCity REST API
Whenever I try this I get an error:
Invoke-RestMethod : The remote server returned an error: (415) Unsupported Media Type.
The local API documentation on the server does not include 415 as a possible response. (...which wouldn't help anyway because the documentation doesn't include any likely causation with the error responses that are in the docs.)
How can I get past this? I could really use a nudge in the right direction.
Here is the code I have tried. Previous attempts are included and commented out.
# docs at
# 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)
# then free up the unmanged memory afterwards
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
# Attempt 1
#$p = @()
#$p += @{
# name = "CAMNamespace"
# value = $userNamespace
# }
#$p += @{
# name = "CAMUsername"
# value = $userName
# }
#$p += @{
# name = "CAMPassword"
# value = $userPassword
# }
#
#$body = @{
# parameters = $p
#}
# Attempt 2
#$body = '{
# "parameters": [
# {
# "name": "CAMNamespace",
# "value": "' + $userNamespace + '"
# },
# {
# "name": "CAMUsername",
# "value": "' + $userName + '"
# },
# {
# "name": "CAMPassword",
# "value": "' + $userPassword + '"
# }
# ]
#}'
# Attempt 3
#$body = '{"parameters": [{"name": "CAMNamespace","value": "' + $userNamespace + '"},{"name": "CAMUsername","value": "' + $userName + '"},{"name": "CAMPassword","value": "' + $userPassword + '"}]}'
# Attempt 4
#$body = "{`"parameters`": [{`"name`": `"CAMNamespace`",`"value`": `"$userNamespace`"},{`"name`": `"CAMUsername`",`"value`": `"$userName`"},{`"name`": `"CAMPassword`",`"value`": `"$userPassword`"}]}"
# Attempt 5
$body = "{
`"parameters`": [
{
`"name`": `"CAMNamespace`",
`"value`": `"$userNamespace`"
},
{
`"name`": `"CAMUsername`",
`"value`": `"$userName`"
},
{
`"name`": `"CAMPassword`",
`"value`": `"$userPassword`"
}
]
}"
$uri = "$protocol`://$serverName`:$port/api/v1/session"
# Attempt 1
#Invoke-RestMethod -Uri $uri -Method Put -Body $body
# Further attempts
Invoke-RestMethod -contentType "text/plain" -Uri $uri -Method Put -Body $body
The Swagger documentation at https://www.ibm.com/docs/swagger/?configuration=SSEP7J_12.0.0/com.ibm.swg.ba.cognos.ca_api.doc/swagger_ca.json#/session/create_update_session gives a little bit more of a clue about how to make a correct request:
If you change your code to:
Invoke-RestMethod -ContentType "application/json" -Uri $uri -Method Put -Body $body
it might get you past the "Unsupported Media" error.
@DanielWagemann's link to the documentation at https://developer.ibm.com/apis/catalog/cognosanalytics--cognos-analytics-rest-api/api/API--cognosanalytics--cognos-analytics#create_update_session also includes a curl
example that you can use to model your Invoke-RestMethod
calls:
curl --request PUT \
--url REPLACE_ENDPOINT_VARIABLE/api/v1/session \
--header 'IBM-BA-Authorization: REPLACE_THIS_VALUE' \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data REPLACE_REQUEST_BODY
See --header 'content-type: application/json'
above which basically suggests using -ContentType "application/json"
for Invoke-RestMethod
as well...