It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to append to a page via curl REST API?

Hi I am extremely new to confluence and was trying to use the REST APIs with cURL.

 

I have been digging around and I never found a clear answer.

From the examples url I was able to do the update with no issues.

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

However it seems to overwrite all of the previous entries.

 

I did find a few posts about doing a expand=body.storage into a variable but it seems to fail with a 500 internal server error. I noticed that if I do something simple without spaces or other characters it works fine.

 

examples of what I'm doing:

export oldcontent=`curl -u $mypass -X GET "https://myconfluence.example.com:8443/rest/api/content/1234567?expand=body.storage"`

 

note something like this works fine.

export simplevar=onewordsimplevar

 

Then I try to an update with the variable.

curl -v -S -u $mypass -X PUT -H 'Content-Type: application/json' --data '{"id":"1234567","type":"page","title":"test","space":{"key":"TST"},"body":{"storage":{"value":" '$oldcontent' <p>This is an updated test from REST API 2</p>","representation":"storage"}},"version":{"number":29}}' https://myconfluence.example.com:8443/rest/api/content/1234567

 

 

In the verbose it has errors like its trying to interpret the content

* Could not resolve host: My oldcontent here

* Closing connection 4

* Could not resolve host: REST; Unknown error

curl: (3) [globbing] unmatched close brace/bracket at pos 88

{"statusCode":500,"message":"","reason":"Internal Server Error"}

 

Any advice? Is there an article with examples how to append to pages?

Thanks

 

 

1 answer

1 accepted

0 votes
Answer accepted

Hi, it's rather difficult to read output here, however it's clear that you have an issue with quotes.

Double quotes must be escaped before passing to curl.

Also you don't have to use "export" variable feature.

Thanks for the response!

Please forgive me, I very new to this I have just been guessing/trying from what I read in other posts. Yes it definitely seems like I'm not closing the quotes properly. 

I have tried single or double quotes variations but get the same error. Doing '"$oldcontent"produces a 500 error as well but does not give the "Could not resolve host ones."

 

If I do not need to use the "export" how would you suggest setting the variable? (just do it without export you mean?)

 

More of the output.

 

* Could not resolve host: David; Unknown error
* Closing connection 0
curl: (6) Could not resolve host: David; Unknown error
curl: (3) [globbing] unmatched close brace/bracket at pos 140
* Could not resolve host: David; Unknown error
* Closing connection 1
curl: (6) Could not resolve host: David; Unknown error
curl: (3) [globbing] unmatched close brace/bracket at pos 140
* About to connect() to xxx.xxx.xxx port 8443 (#2)
* Trying xx.xx.xx.xx...
* Connected to xxx.xxx.xxx (xx.xx.xx.xx) port 8443 (#2)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* skipping SSL peer certificate verification
* SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate:
* subject: CN=xxx.xxx.xxx,OU=xx,O=xx,L=xx,ST=xx,C=xx
* start date: Jul 09 20:29:56 2019 GMT
* expire date: Jul 13 20:29:56 2022 GMT
* common name: xxx.xxx.xxx
* issuer: CN=xxx.xxx.xxx,OU=xxx,O=xxx,L=xxx,ST=xxx,C=xx
* Server auth using Basic with user 'david@example.com'

I suggest you to do next:

- execute curl and assign result to variable

oldcontent=$(curl -u $mypass -X GET "https://myconfluence.example.com:8443/rest/api/content/1234567?expand=body.storage")

 - print content of above variable

echo $oldcontent

I'm sure that output won't be exactly what you expect to see, it'll have not only content of your page but also other fields and probably a lot of quotes. This means that you are not ready to continue with updating page. Before that you have to do next:

- extract value field from curl command. Content of value it's actually storage format of your page:

curl -u $mypass -X GET "https://myconfluence.example.com:8443/rest/api/content/1234567?expand=body.storage" | python -mjson.tool | grep value

- escape all double quotes in above output. This might give you a base idea:

$echo "text\"text"
text"text
$ echo "text\"text" | sed 's/\"/\\"/g'
text\"text

curl -u $mypass -X GET "https://myconfluence.example.com:8443/rest/api/content/1234567?expand=body.storage" | python -mjson.tool | grep value | sed 's/\"/\\"/g'

- if output is ok then assign it to variable:

oldcontent=$(curl -u $mypass -X GET "https://myconfluence.example.com:8443/rest/api/content/1234567?expand=body.storage" | python -mjson.tool | grep value | sed 's/\"/\\"/g')

- next you have to find current page version and increment it by one. You can skip automation here right now and just do it manually by looking for the current version and constructing update command with new value

Its odd if have an additional lines is fails with 500 Internal Server Error.

I was able to set the oldcontent variable with single of html code and it worked without issues.

Is my cURL get command wrong?

oldcontent=`curl -k -u $mypass -X GET https://myexample.example.com:8443/rest/api/content/1234567?expand=body.storage`

It seems it contains multiple lines with the {id}{properties etc}

It looks like my extended answer to your previous post which I made yesterday just disappeared.

Weird Community system, I spent about half an hour to write it...

Yes I noticed my reply disappeared the other day too. At the time I was having an internet issue so I assumed that was why. I ended up re-posting as well. I probably am the most sorry it disappeared.

Thanks Pavel, this was the last piece I was missing.

Strangely I was under the impression that I need those tags, but without them it makes much more sense.

One more question.

It works great for some of the simpler pages.

However some produces a 500 error. I suspect some of the xhtml code cases the error. 

The ones with code blocks or other formatting cause errors. Is there a list of characters that need to be stripped out in order for it work?

Thanks again 

It looks like after some downtime with Community Forum my post has been restored))

It's hard to tell what exactly causes 500 error. For production use you have to consider other way of POSTing data with curl. You don't have to append raw data to --data key in curl. Instead you have to construct json file and pass it to curl.

In that file you have to escape all double quotes only under VALUE tag.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

What project did you transition or start on Confluence with the shift to remote work?

It’s been great to hear from fellow users over the last few weeks about the best tips and fun moments you’ve had working on Confluence since the transition to working remote. I’d love to keep the c...

32 views 2 4
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you