Add Attachment to Confluence page

Hi there,

I am currently trying to automatically add file attachments to a confluence page using script runner.

As a first test I used the "create page in confluence" example and tried to extend this with the confluence rest api description. But to no success.

I tried to utilize org.apache.http.entity.mime.MultipartEntityBuilder, but now I get a RuntimeException that no body writer was found for this class.

Perhaps this is not the right approach or am I missing something?

A code snippet:

def pathName = "C:\\reports\\testreport.pdf"
File fileUpload = new File(pathName)
assert fileUpload
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create()
assert entityBuilder
entityBuilder.addPart("file", new FileBody(fileUpload))
// Child Attachment Example test
authenticatedRequestFactory
    .createRequest(Request.MethodType.POST, "rest/api/content/" + masterPageId + "/child/attachment")
    //.addHeader("Content-Type", "application/json")
    //.setRequestBody(new JsonBuilder(params).toString())
	.setEntity(entityBuilder.build())
    .execute(new ResponseHandler<Response>() {
        @Override
        void handle(Response response) throws ResponseException {
            if(response.statusCode != HttpURLConnection.HTTP_OK) {
                throw new Exception(response.getResponseBodyAsString())
            }
        }
})

 

Hopefully somebody already did that in the past?

 

Cheers,

Michel

1 answer

Well,

I found it out myself.

Instead of usign the apache MultipartEntityBuilder I used the Multipart approach of the Shared Access Layer...

 

Working code:

def pathName = "C:\\reports\\testreport.pdf"
File fileUpload = new File(pathName)
assert fileUpload
def part = new RequestFilePart(fileUpload, "file")
assert part
def partList = new ArrayList<RequestFilePart>()
partList.add(part)
assert partList
assert partList.size() == 1
// Child Attachment Example test
authenticatedRequestFactory
    .createRequest(Request.MethodType.POST, "rest/api/content/" + masterPageId + "/child/attachment")
    .addHeader("X-Atlassian-Token", "nocheck")
    //.setRequestBody(new JsonBuilder(params).toString())
	//.setEntity(entityBuilder.build())
	.setFiles(partList)
    .execute(new ResponseHandler<Response>() {
        @Override
        void handle(Response response) throws ResponseException {
            if(response.statusCode != HttpURLConnection.HTTP_OK) {
                throw new Exception(response.getResponseBodyAsString())
            }
        }
})

Cheers,

Michel

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

172 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you