Fetching page content with REST API v2

Juan Leon November 27, 2024

I need help just getting a heartbeat with API v2 when attempting to fetch page content.  Any example of working code that would be the equivalent for the following would be appreciated!

All previous efforts have resulted in pages with content being reported to have none.

The legacy code below works just fine.

 

 

Map<String, Object> performApiRequest(String endpoint, Map<String, String> queryParams) {
    if (!endpoint || !(queryParams instanceof Map)) {
        throw new IllegalArgumentException("Invalid endpoint or query parameters. Ensure both are provided correctly.")
    }

    println("INFO: Initiating API request to endpoint: ${endpoint}")
    try {
        // Perform the GET request
        def response = get(endpoint)
                          .queryString(queryParams as Map)
                          .header("Content-Type", "application/json")
                          .asObject(String) // Response body is expected as a raw JSON string

        // Check for a successful response status
        if (response?.status == 200) {
            println("INFO: API call to ${endpoint} succeeded with status: 200")
           
            // Parse JSON response body to a Map
            def body = response.body
            if (body && body instanceof String) {
                return new JsonSlurper().parseText(body) as Map<String, Object>
            } else {
                println("ERROR: Response body is missing or invalid.")
                return null
            }
        } else {
            println("ERROR: API call to ${endpoint} failed with status: ${response?.status ?: 'unknown'} - ${response?.statusText ?: 'unknown'}")
        }
    } catch (Exception e) {
        println("ERROR: Exception during API call to ${endpoint}: ${e.message}")
    }

    return null // Return null if any part of the process fails
}
Thank you.

2 answers

0 votes
Jim Knepley - ReleaseTEAM
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
November 27, 2024

Hi @Juan Leon 

I suspect that you need to add a "body-format" parameter to your API call. When I call that API endpoint without that parameter, the body response field is empty.

The "export_view" parameter value generates an HTML partial, which might be easiest to process.

ref: https://developer.atlassian.com/cloud/confluence/rest/v2/api-group-page/#api-pages-id-get

0 votes
Marc - Devoteam
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.
November 27, 2024

Hi @Juan Leon 

If this is regarding development of code for an app, its best to raise your request on the developer forum.

https://community.developer.atlassian.com/ 

This post my help you as well: how-can-i-access-confluence-page-content-through-the-confluence-rest-v2-api 

Juan Leon November 27, 2024

Thanks you for the pointer.  Will do.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events