Add labels on content create

I’ve been researching and I’ve seen a lot of threads saying that you have to create a page first and then add a label, but in the Confluence API docs (https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-label-post) It says that labels can be added when creating content and then just points at the create content API, but with no reference with how to add a label param.

Trying to add a label on page create. If it has to be added after page create, please advise the way to get the pageID for the newly created page so I can make the call to add labels.

I’m doing all development in Groovy, here is the code:

import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.JSON
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import groovyx.net.http.HttpResponseException
import groovyx.net.http.HttpResponseDecorator
import java.util.Base64
import java.util.Base64.Encoder
import com.atlassian.confluence.event.events.content.page.PageEvent


def event = event as PageEvent

def url = "http://localhost:8090/rest/api/content/"

def client = new RESTClient(url)
//Change to match your username/password
def authBasic = Base64.getEncoder().encodeToString(("user"+":"+"password").getBytes())



def date = new Date().format( 'yyyyMMdd' )


def employee = new JsonSlurper().parseText('{"type":"page","title":"Employee Page for ' + date + '","ancestors":[{"id":819224}],"space":{"key":"COM"},"body":' + '{"storage":{"value":"<ac:structured-macro ac:name=\\"excerpt\\"><ac:parameter ac:name=\\"hidden\\">true<\\/ac:parameter><ac:parameter ac:name=\\"atlassian-macro-output-type\\">BLOCK<\\/ac:parameter><ac:rich-text-body><p>Enter your status in this block.<\\/p><\\/ac:rich-text-body><\\/ac:structured-macro>","representation":"storage"}}}')
def manager = new JsonSlurper().parseText('{"type":"page","title":"Manager Roll up for ' + date + '","ancestors":[{"id":819222}],"space":{"key":"COM"},"body":{"storage":{"value":"<p>Here are all of my direct statuses</p>","representation":"storage"}}}')


try{
    HttpResponseDecorator response = (HttpResponseDecorator) client.post(path: "/rest/api/content/",
            contentType: JSON,
            body: employee,
            headers: [Accept: 'application/json',Authorization:"Basic ${authBasic}"])


    log.error("Status: " + response.status)
    if (response.data) {
        log.error("Content Type: " + response.contentType)
        log.error("Headers: " + response.getAllHeaders())
        log.error("Body:\n" + JsonOutput.prettyPrint(JsonOutput.toJson(response.data)))
        println(response.data.id)
    }
}
catch(HttpResponseException e){
    log.error e.getResponse().getData()
}




try{
    HttpResponseDecorator response = (HttpResponseDecorator) client.post(path: "/rest/api/content/",
            contentType: JSON,
            body: manager,
            headers: [Accept: 'application/json',Authorization:"Basic ${authBasic}"])

    log.error("Status: " + response.status)
    if (response.data) {
        log.error("Content Type: " + response.contentType)
        log.error("Headers: " + response.getAllHeaders())
        log.error("Body:\n" + JsonOutput.prettyPrint(JsonOutput.toJson(response.data)))
    }
}
catch(HttpResponseException e){
    log.error e.getResponse().getData()
}

Hey there again!

The page id will be in your response.data and then you can use the Add Label rest method.

Regards,

Hi @italo.qualisoni I’m struggling with grabbing the page id from the response. This is 100% me being a beginner with Java. I can see the response in the logs and I can see the ID, but I can’t figure out how to store the id, or even print it out isolated, so that I can use it in in a subsequent call. Appreciate any direction.

@italo.qualisoni I decided to change to Bash as I’m more comfortable with it and I was able to get the Page id and add the label simply.

Here is a script in case anyone is in the same spot:

#!/bin/bash

user=enteruser
password=enterpass

createpage=$(curl -u $user:$password -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"With Labels23","space":{"key":"COM"},"body":{"storage":{"value":"&lt;p&gt;This is a new page&lt;/p&gt;","representation":"storage"}}}' http://localhost:8090/rest/api/content/ | jq '.id')

pageid="${createpage%\"}"
pageid="${pageid#\"}"

echo $pageid;

curl -u $user:$password -X POST -H 'Content-Type: application/json' -d'{ "prefix": "global", "name": "NewLabel"}' http://localhost:8090/rest/api/content/$pageid/label
1 Like

@coreygans,

Great! This scripting can be done in any programming language, great you have found one that your more familiar with.