How can I automatically update a Confluence page via a script... rest api not working

Andrew Pohl November 13, 2017

I am trying to set up a script I have to send a specific type of file to a Confluence page I have set up.  I have tried to use the REST API information that is provided, but I am not sure if I am setting it up correctly.  For now I am just trying to use a curl command since the script I have is written in BASH.  

 

The specific page url looks like (nearly identical to) this: https://<my-company>home.atlassian.net/wiki/spaces/TA/pages/<9-digit-number-here>/Recent+Test+Results

 

My thought would be that I would need to use the "Upload an attachment" endpoint as seen here, but I am not sure if I am configuring it correctly.  Thoughts?  I have tried plugging in values for the different pieces of the endpoint url, but not sure if they are correct.  Thanks in advance

https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples

1 answer

1 accepted

3 votes
Answer accepted
Anton Chemlev - Toolstrek -
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 14, 2017

Hi,

Could you please provide some details:

1) What Confluence do you use: cloud or server? And version. Because you link looks like Cloud, but link to Confluence docs you provided is for Server version.

2) What does it mean - "not working"? Could you provide full curl command and error text?

3) What type and size of file are you trying to upload? Does it fit into system allowed Attachment maximum size? By default it is 100MB, but maybe you changed it.

Andrew Pohl November 14, 2017

Hi Anton,

  Thank you for replying.  To answer your questions:

1.) I wasn't sure at first if I was using Cloud or Server (had never really used this service before so couldn't really tell), but I've discovered that it is the Cloud.  I've since gone into that documentation and seen what I can use.... however, still unsure of how to proceed.

I am hoping to upload an attachment to a specific page that I created.  Upon looking through the documentation I believe that I should be trying this curl (from https://developer.atlassian.com/cloud/confluence/rest/#api-content-id-child-attachment-post):

curl -D- \
  -u admin:admin \
  -X POST \
  -H "X-Atlassian-Token: nocheck" \
  -F "file=@example.txt" \
  -F "minorEdit=true" \
  -F "comment=Example attachment comment" \
  http://myhost/rest/api/content/123/child/attachment

I tried uploading a simple txt file as an attachment, but still not sure if I am adding the correct values for the specific page.  I assume that the url should look something like this:

curl -D- \

-u <my-user-email>:<my-password> \

-X POST \

-H "X-Atlassian-Token: nocheck" \

-F "file=@<name of file to send>" \

-F "minorEdit=true" \

-F "comment=Trying this out" \

http://augusthome.atlassian.net/rest/api/content/<9-digit-number-which-I-assume-is-the-space-id>/child/attachment

 

When I submitted this in my terminal I got an error of:

(26) couldn't open file "firmware.txt"

I will continue to play around with this, but I'm a little confused about why the "@" needs to be before the filename, and if I am actually pointing to the correct place.  

The files that I am trying to upload ultimately are just simple PNG files, about 6911 bytes.  

Andrew Pohl November 14, 2017

To give you an example of what I had been getting prior to this most recent attempt, here is the curl command and the response I was getting before:

Curl command:

curl -v -S -u <username>:<password> POST -H "X-Atlassian-Token: no-check" -F "file=Lock_Firmware/CHART/EUROPA/EUROPA-1.7.8-2017-11-01-12-37-16.png" -F "comment=11/09/2017 EUROPA 1.7.7" "http://<myhost>/rest/api/content/211845263/child/attachment"

 

Response:

* Rebuilt URL to: POST/

* Could not resolve host: POST

* Closing connection 0

curl: (6) Could not resolve host: POST

*   Trying <my-host-ip>...

* TCP_NODELAY set

* Connected to <myhost>(<myhost-ip>) port 80 (#1)

* Server auth using Basic with user '<my-username>'

> POST /rest/api/content/211845263/child/attachment HTTP/1.1

> Host: <myhost>

> Authorization: Basic <some-hash>

> User-Agent: curl/7.54.0

> Accept: */*

> X-Atlassian-Token: no-check

> Content-Length: 321

> Expect: 100-continue

> Content-Type: multipart/form-data; boundary=------------------------2bda88f822a47675

>

* Done waiting for 100-continue

< HTTP/1.1 301 Moved Permanently

< Content-Type: text/html

< Date: Tue, 14 Nov 2017 21:32:06 GMT

< Location: https://<myhost>/rest/api/content/211845263/child/attachment

< Connection: Keep-Alive

< Content-Length: 0

* HTTP error before end of send, stop sending

<

* Closing connection 1

Anton Chemlev - Toolstrek -
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 20, 2017

Hi,

1) About @ in curl:

--data-binary <data>
  (HTTP) This posts data exactly as specified with no extra processing whatsoever.
  If you start the data with the letter @, the rest should be a filename.  Data is
  posted in a similar manner as --data-ascii does, except that newlines are preserved
  and conversions are never done.

  If this option is used several times, the ones following the first will append data
  as described in -d, --data.

 2) (26) couldn't open file "firmware.txt" - try to use absolute file to path

3) It seems to me there's an error. URL should be in -X option as far as i know.

curl -D- \
  -u admin:admin \
  -X POST  http://myhost/rest/api/content/123/child/attachment \
  -H "X-Atlassian-Token: nocheck" \
  -F "file=@example.txt" \
  -F "minorEdit=true" \
  -F "comment=Example attachment comment"
 
Andrew Pohl November 20, 2017

Anton,

  Hello again.  Thank you for that explanation, you've been most helpful.  I was able to figure out what I needed to do in order to successfully create an attachment.  It did not end up doing what I expected, but the attachments are in fact there and I can view them.  I was expecting the attachment (which in my case is a PNG file) to actually appear on the page as the image itself, and not just as a linked attachment in the attachments specific page that is linked to my content id.  

  In other words, I'm trying to just add an image to the page itself, not an attachment.  I'm unsure based on what I am seeing in the CLOUD REST API docs as to how I can do that.... all I seem to be able to do is add text (which is what a co-worker of mine was able to do on another Confluence page in our company's main space).  Any thoughts on how I could configure the Create Content endpoint to accept an image?

https://developer.atlassian.com/cloud/confluence/rest/?_ga=2.80213663.941474478.1510795166-846788406.1503103717#api-content-post

Thank you in advance

Anton Chemlev - Toolstrek -
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2017
Andrew Pohl November 21, 2017

Anton,

  Ok cool.  I will look into how that works and see if I can make something work.  I do appreciate your help, and I'm glad that I at very least am now able to successfully attach the image to the page correctly.  Cheers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events