Can access REST API content via web browser but invoking REST request from terminal returns 0 result

Jeremiah Watkins March 20, 2023

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` in PowerShell, using the exact same credentials and exact same URI, it's successful but there are 0 results:

$uri = "https://<confluence server>/confluence/rest/api/content/search?cql=type=page+and+space=SpaceName"
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 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 programmatically/PowerShell?

I found this question which is similar but that user is getting an error that appears to be a credential issue. I'm not getting any errors when I query from PowerShell, and I actually get content returned but the results are empty. I've even tested incorrect credentials and it throws an error, so I know the credentials I'm using are correct since I wouldn't get anything back if it wasn't.

Anyone know what's going on here?

1 answer

1 accepted

0 votes
Answer accepted
Jeremiah Watkins March 20, 2023

I was using the `-Credential` parameter of `Invoke-RestMethod` (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, PowerShell's `Invoke-RestMethod` isn't passing the `PSCredential` credentials correctly with the `-Credential` parameter. 

David Bakkers
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2023

Hello @Jeremiah Watkins 

With the Invoke-RestMethod, the -Credentials parameter, when used just by itself as you tried, only works when the remote service sends an authentication challenge request, which Confluence's REST API service doesn't do for Basic authentication requests; those requests are received passively.

You can use the -Authentication parameter in conjunction with the -Credentials parameter to declare the connection type... but just putting the Base64 encoded key:value pair directly into the Authorisation section of the header using the -Headers parameter is the easiest way to go... as you've already discovered :)

Like Jeremiah Watkins likes this
Jeremiah Watkins March 20, 2023

@David Bakkers I didn't know about the -Authentication parameter, thanks for pointing that out!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events