Check if child page already exists using Confluence REST API (JavaScript)

Currently I have an HTML button macro that makes a POST request with Fetch to create a new child page. The issue is that I have to delete the child page each time before creating it again. I would like to be able to check if the child page already exists (by page title I suppose) and to either make a PUT request to update the page with new information or to just DELETE it and create a new page.

Below is my pageExists function, the issue is that I’m not able to see the child pages in the response. The idea is to loop through the child pages and find a single child page which has a certain title, if that page exists then return true, if not return false.

let pageExists = function (parentId) {
    let myUrl = `http://svapptest:8090/confluence/rest/api/content/${parentId}/child`

    fetch(myUrl, {
        method: 'GET',
        headers: {
            'Accept': 'application/json'
        }
    }).then(response => response.json())
        .then(data => console.log(data))
}

Right now the response I get is:
child-return-value

I also get this response when adding /descendant in place of /child to the end of the URL.

Hi @Laurence

change:

let myUrl = `http://svapptest:8090/confluence/rest/api/content/${parentId}/child`

to

let myUrl = `http://svapptest:8090/confluence/rest/api/content/${parentId}/child?expand=page`

doc → https://docs.atlassian.com/ConfluenceServer/rest/7.14.0/#api/content/{id}/child-children

or

let myUrl = `http://svapptest:8090/confluence/rest/api/content/${parentId}/child/page`

doc → https://docs.atlassian.com/ConfluenceServer/rest/7.14.0/#api/content/{id}/child-childrenOfType

BTW - the page title is unique in space, you can find page by title in the space using this API

let url = "http://localhost:1990/confluence/rest/api/content?title=Test page&spaceKey=AAA"
let username = "admin"
let password = "admin"

fetch(url, {
    method: "GET",
    headers: {
        "Authorization": "Basic " + btoa(username + ":" + password), 
        "Content-Type": "application/json"
    }
})
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));

Cheers
Adam

2 Likes

Thank you! It’s working now