powershellconfluence-rest-api

Confluence REST API content not returning results with `Invoke-RestMethod`, but results are visible via web browser


I have a Confluence site and I want to pull information about the pages via the Confluence REST API. I'm using a CQL query: cql=type=page and space=SpaceName, so my URI is this:

https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName

I can log into Confluence from a web browser, then navigate to the above URL and I see all the content in JSON format.

However, when I run Invoke-RestMethod using the exact same credentials and exact same URI, it's successful but there are 0 results:

PS > $uri = "https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName"
PS > Invoke-RestMethod -Uri $uri -Credential $credential -Method Get

results        : {}
start          : 0
limit          : 25
size           : 0
cqlQuery       : type=page and space=SpaceName
searchDuration : 20
totalSize      : 0
_links         : @{self=https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName; base=https://<confluence server>/confluence; context=/confluence}

I've also passed the output to a variable ($results) and checked the results property; it is empty:

PS > $results = Invoke-RestMethod -Uri $uri -Credential $credential -Method Get
PS > [string]::IsNullOrEmpty($results)
True

I don't know if the issue is with the Confluence server not acting right, my credentials, or Invoke-RestMethod. Is it possible that the Confluence server could be permitting queries/providing different results via a web browser but somehow not via PowerShell?

Anyone know what's going on here?


Solution

  • Got it working. I was using the -Credential param with PSCredential (which has worked for me in the past with Bitbucket, Confiforms, Jira, etc.). When I switched to the -Headers parameter with Basic Auth, it started working as expected:

    PS > $uri = "https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName"
    PS > $Headers = @{
        Authorization = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("username:password"))
    }
    PS > Invoke-RestMethod -Uri $uri -Headers $Headers -Method Get
    
    results        : {@{id=514598038; type=page; status=current; title=page-title | 20230126-093349 | b05f41c; restrictions=; _links=; _expandable=}, @{id=514428061; type=page; status=current;...}
    start          : 0
    limit          : 25
    size           : 25
    cqlQuery       : type=page and space=SpaceName
    searchDuration : 36
    totalSize      : 6642
    _links         : @{self=https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName; next=/rest/api/content/search?limit=25&start=25&cql=type=page+and+space=SpaceName; base=https://<confluence server>/confluence; context=/confluence}
    

    Somehow, Invoke-RestMethod isn't passing the PSCredential credentials correctly with the -Credential parameter.