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()
}