Get a page by its title

The API v1 Get content call (/wiki/rest/api/content) is deprecated. We are currently using this call to get a page by its title, e.g.:

/wiki/rest/api/content?spaceKey=MYFIRSTSPACE&title=My first space Home

Is there an alternative in the v2 API?

I’ve already tried adding parameters to the Get pages and Get pages in space calls, but those don’t work:

/wiki/api/v2/pages?spaceId=65758&title=My first space Home

and

/wiki/api/v2/spaces/65758/pages?title=My first space Home
2 Likes

@t.bundt

I would use the search content endpoint (which is not deprecated) and query the page using CQL instead e.g.

/wiki/rest/api/content/search?cql=space="MYFIRSTSPACE" and title="My first space Home"

@rcsr
I guess I have to use it, but I’m not exited about it.
For one, now I will have to migrate twice, once to the v1 CQL search and then a second time to the v2 version, whether that will be a CQL search or something similar.
Also the CQL search is slower (129ms to 295ms) compared to my old search (83ms to 108ms).

How do you search for a page with this title: A “test” page

When trying /wiki/rest/api/content/search?cql=title="A "test" page"
I get com.atlassian.confluence.api.service.exceptions.BadRequestException: Unrecognised clause at : test" page"

When trying /wiki/rest/api/content/search?cql=title="A \"test\" page"
I get a 400 error directly from the Apache Tomcat

When trying /wiki/rest/api/content/search?cql=title=%22A %5C%22test%5C%22 page%22
I get com.atlassian.confluence.api.service.exceptions.SSStatusCodeException: CQL was parsed but the search manager was unable to execute the search. Error message: com.atlassian.confluence.api.service.exceptions.SSStatusCodeException: There was an illegal request passed to XP-Search Aggregator API : HTTP/1.1 400 Bad Request

It looks like this is a bug in Confluence: https://jira.atlassian.com/browse/CONFCLOUD-52615

@t.bundt
Yes I think this is an outstanding bug. One workaround you could do is to first sanitize the query using a regex to remove all the special characters and use the CONTAINS operator instead of the EQUALS operator like so:

const title = `A "test" page`.replace(/[^a-zA-Z0-9 ]/g, "");
// A test page
const res = await AP.request(`/rest/api/content/search?cql=title~"${title}"`);
console.log(JSON.parse(res.body));

The above query worked for me when I tested it and should hopefully get you close enough to the desired page.

For other special characters like [ ] you can sanitize them using encodeURIComponent
e.g. A [test] page becomes wiki/rest/api/content/search?cql=title="A%20%5Btest%5D%20page" and returns back the expected page even when using the EQUALS operator.

This doesn’t work for double quotes though, unfortunately, so it doesn’t solve your initial problem with A "test" page.

Seeing as the bug may never be fixed, I would do some defensive programming in the meantime to handle these special characters. The full list of special characters can be found here.

1 Like

:wave: @t.bundt Thanks for the feedback here!

This is actually something that is around the corner - we are targeting a release in early July that would include support for this. However, the changes recently made it to prod :slight_smile:

Could you try the above again and see if it works for you?

1 Like

Both

/wiki/api/v2/pages?spaceId=65758&title=My first space Home

and

/wiki/api/v2/spaces/65758/pages?title=My first space Home

do now work. That’s great!

1 Like