How to append to a page via curl REST API?
Asked by David:

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


Answers:
author: Pavel Potcheptsov
body: 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.

comments: author: David
body: 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'


author: Pavel Potcheptsov
body: 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


author: David
body: 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}


author: Pavel Potcheptsov
body: 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...


author: David
body: 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.


author: David
body: 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.


author: David
body: 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


author: Pavel Potcheptsov
body: 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.


Tags: confluence-server

curl

rest-api

server

update