Can access REST API content via web browser but invoking REST request from terminal returns 0 result
Asked by Jeremiah Watkins:

I have a Confluence site and I want to pull information about the pages via the [Confluence REST API](https://developer.atlassian.com/server/confluence/confluence-server-rest-api/). I'm using a [CQL query](https://developer.atlassian.com/server/confluence/advanced-searching-using-cql/): \`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](https://community.atlassian.com/t5/Jira-Core-Server-questions/I-can-get-JSON-results-from-the-browser-but-not-through/qaq-p/971091) 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?


Answers:
author: Jeremiah Watkins
body: 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.

comments: author: David Bakkers
body: Hello [@Jeremiah Watkins](/t5/user/viewprofilepage/user-id/5105948)

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 :)


author: Jeremiah Watkins
body: [@David Bakkers](/t5/user/viewprofilepage/user-id/1575730) I didn't know about the -Authentication parameter, thanks for pointing that out!


Tags: confluence-server

rest-api

server