How to upload a file to Atlassian Confluence page using curl
Asked Answered
K

4

9

I am trying to upload an .xls file to a Confluence wiki page following the guidelines given in the Remote API documentation : https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples#ConfluenceRESTAPIExamples-Uploadanattachment

curl -v -S -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "[email protected]" -F "comment=this is my file" "http://localhost:8080/confluence/rest/api/content/3604482/child/attachment" | python -mjson.tool

This is what I am doing:

curl -v -S -u username:password -X POST -H "X-Atlassian-Token: nocheck" -F "file=@/path/to/local/excelsheet.xls" https://<Confluence server>/display/page

I omitted the Python -mjson.tool as it says 'No JSON object could be decoded' and it didn't make sense as I am not posting JSON.

However the above curl command is not working for me. I see the html of the target page on my console and the file doesn't get uploaded. I tried modifying the curl command in several ways but nothing worked.

Also for the URL of the page I am trying to upload to, it doesn't have any contentID as suggested in the documentation. The target URL is a page which accepts attachments and shows the uploaded file list.

Could someone please point out where am I going wrong? I don't have much experience with Curl.

Kymry answered 21/3, 2014 at 6:23 Comment(0)
L
3

you need to use REST API in url: .../confluence/rest/api/content/$PAGE_ID/child/attachment and now you are using url of view page.

Langton answered 21/3, 2014 at 6:40 Comment(9)
I tried this as well. It didn't work :( I looked up the page_id from the Page Information section under Tools.Kymry
What exactly did you try? Also, make sure that: 1) rest api is enabled by admin (by default it's off IIRC), 2) you have rights to use it, 3) it is not restricted to intranet/whitelist/otherwise_authorized_users, 4) it is under the same host as 'viewable' confluence.Langton
I tried curl -v -S -u username:password -X POST -H "X-Atlassian-Token: nocheck" -F "[email protected]" -F "comment=this is my file" "https://<confluence server>/confluence/rest/api/content/889912330/child/attachment" 889912330 is the page id that I found when I looked it up under Tools. On the console, it returns a 404 along with all the html.Kymry
you should wrap your comment with single quotes: curl -v -S -u username:password -X POST -H "X-Atlassian-Token: nocheck" -F "[email protected]" -F "comment='this is my file'" "http://<confluence server>/confluence/rest/api/content/889912330/childLangton
I removed the comments altogether. curl -v -S -u username:password -X POST -H "X-Atlassian-Token: nocheck" -F "[email protected]" "https://<confluence server>/confluence/rest/api/content/889912330/child/attachment. Still not working :(Kymry
Are you completely sure that you have rest api enabled on your confluence and have rights to use it?Langton
I used the test given in this link #6853816 ..I could see the blank page. I am also shooting an email to the admin to make it very sure that Remote API is enabled and I can access it.Kymry
I'll update it here what I hear from the admin. Thanks for your help!Kymry
The admin responded. Turns out that Confluence Remote API is blocked on our network! But nevertheless, thanks a lot for helping me with this.Kymry
G
4

David Vonka answer is correct except for header "X-Atlassian-Token" value. It must be "no-check" (instead of "nocheck")

So corrected command is:

curl -v -S -X POST -H "X-Atlassian-Token: no-check" -F "file_0=@<file name>" -F "comment_0=<upload comment>" "http://<server>:<port>/<context>/pages/doattachfile.action?pageId=<page id>&os_username=<username>&os_password=<password>"

NOTE: replace all the <...> placeholders with your values

Gerontocracy answered 15/4, 2014 at 12:0 Comment(0)
L
3

you need to use REST API in url: .../confluence/rest/api/content/$PAGE_ID/child/attachment and now you are using url of view page.

Langton answered 21/3, 2014 at 6:40 Comment(9)
I tried this as well. It didn't work :( I looked up the page_id from the Page Information section under Tools.Kymry
What exactly did you try? Also, make sure that: 1) rest api is enabled by admin (by default it's off IIRC), 2) you have rights to use it, 3) it is not restricted to intranet/whitelist/otherwise_authorized_users, 4) it is under the same host as 'viewable' confluence.Langton
I tried curl -v -S -u username:password -X POST -H "X-Atlassian-Token: nocheck" -F "[email protected]" -F "comment=this is my file" "https://<confluence server>/confluence/rest/api/content/889912330/child/attachment" 889912330 is the page id that I found when I looked it up under Tools. On the console, it returns a 404 along with all the html.Kymry
you should wrap your comment with single quotes: curl -v -S -u username:password -X POST -H "X-Atlassian-Token: nocheck" -F "[email protected]" -F "comment='this is my file'" "http://<confluence server>/confluence/rest/api/content/889912330/childLangton
I removed the comments altogether. curl -v -S -u username:password -X POST -H "X-Atlassian-Token: nocheck" -F "[email protected]" "https://<confluence server>/confluence/rest/api/content/889912330/child/attachment. Still not working :(Kymry
Are you completely sure that you have rest api enabled on your confluence and have rights to use it?Langton
I used the test given in this link #6853816 ..I could see the blank page. I am also shooting an email to the admin to make it very sure that Remote API is enabled and I can access it.Kymry
I'll update it here what I hear from the admin. Thanks for your help!Kymry
The admin responded. Turns out that Confluence Remote API is blocked on our network! But nevertheless, thanks a lot for helping me with this.Kymry
M
1

I do not think that confluence rest api allows file upload. Please do this instead

curl -v -S -X POST -H "X-Atlassian-Token: nocheck" -F "file_0=@<file name>" -F "comment_0=<upload comment>" "http://<server>:<port>/<context>/pages/doattachfile.action?pageId=<page id>&os_username=<username>&os_password=<password>"

replace all the <...> placeholders with your values

Mccaleb answered 22/3, 2014 at 13:42 Comment(1)
works for me on Confluence 3.5, the newer version do indeed have a res api that allows upload.Mccaleb
P
0

The API POST syntax should be corrected from "https://ConfluenceServer/display/page" to the correct res/api/content syntax eg: "https://companyName.atlassian.net/display/rest/api/content/pageIDxxxxx":

 curl -v -S  -X POST -H "X-Atlassian-Token: no-check" -F "file_0=@<file name>" -F "comment_0=<upload comment>" https://<companyName>.atlassian.net/display/rest/api/content/<pageID15398762>/child/attachment

The pageID can be found in the URL display page. For example: https://companyName.atlassian.net/display/spaces/DEV/pages/pageID15398762/Page+Title+Name

For more API syntax details please refer to this documentation:
https://docs.atlassian.com/atlassian-confluence/REST/6.6.0/#content-createContent

Prizewinner answered 17/10, 2019 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.