You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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
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
HI Michel,
Can you help me to achieve this in cloud Confluence ?
I have create script which will give me link for attachment on confluence page but not actual attachment! Can you help on this
Regards,
Shubham
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.