confluence REST API, update attachment comments
Asked by Alex Perez:

Hello!

I'm developing a script to help us in a content migration.

The goal is :

-list all pages in a space,

-for each page, list all attachment

-for each attachment, find out the filename in a table and (if found) add a comment to the attachment (extracted from the external table).

I'm facing problems with the last step, I can get all attachments in a page by calling:

GET /rest/api/content/%pageID%/child/attachment

returns an array of 2 attachments (ok).

each attachment contains id, filename, metadata (Versions, author ..etc). Attachment ID format is "**att**16257549" (??)

I'm trying PUT /content/%pageID%/child/attachment/%attachmentId%

But seems like the attachment ID is not the same format as the previous step.

How can I add the comment to the attachment?

----- EDIT ----

in order to clarify the example

Im sending a PUT /content/%pageID%/child/attachment/**att16259572**

with this payload:

```
{"id":"att16259572","type":"attachment","status":"current","title":"Path LUN is dead.doc","ancestors":[],"operations":[],"children":[],"descendants":[],"body":[],"restrictions":[],"metadata":{"comment":"Path LUN is dead"}}
```

getting HTTP 400.

(and other variations, but the goal is only updating the attachment comment.)


Answers:
author: fwoon
body: i always try to do a curl to verify then update the python code

```
curl -u <USERNAME>:<PASSWORD> -X PUT -H 'Content-Type: application/json' -d'{"id":"123456789","version":{"number":2},"metadata": {"comment": "HAHAH"}}' <BASE_URL>/rest/api/content/<PAGE_ID>/child/attachment/<ATTACHMENT_ID> | python -mjson.tool
```

```
def update_attachment(pageID, attachment_id, comment, version):
url = BASE_URL + pageId + "/child/attachment" + attachment_id
data = json.dumps({ "id": attachment_id, "version": {"number":version+1}, "metadata": {"comment": comment} })
headers = { "X-Atlassian-Token": "nocheck", 'Content-Type' : 'application/json' }
r = requests.put(url, data=data, headers=headers, auth=(username, password))
r.raise_for_status()
print r.text
```

Both curl and function works  
**the addition of att to the attachment id is NOT accurate...DO NOT ADD that att to the attachment id in the data nor the url**

comments: author: Alex Perez
body: god bless you!

i dont know where comes the "att" prefix from, but worked like a charm now


author: Stephen Sifers
body: Hello Alex,

Thank you for providing the entire concept of what you're working on to include your steps and which step and endpoint you're having issues with. Clearly, you know what you're doing when it comes to scripting and API calls.

From reviewing your notes you have, It seems that when you're making the PUT call to the endpoint of [PUT /rest/api/content/{id}/child/attachment/{attachmentId}](https://docs.atlassian.com/ConfluenceServer/rest/6.14.1/#api/content/%7Bid%7D/child/attachment-update) that you're missing the version you want to update the attachment to. Each time we make a change to an attachment, to include comments, you have to change the version by an increment of one. If the attachment was version 1 and you make a PUT call to add a comment the version must increment to version 2. If the version is not changed, this would explain why you're getting a STATUS 400 response.

As an example, if your version was 1 when you made the GET call then your PUT call payload would look as follows (Please note that version is now included).

```
{"id":"att16259572","type":"attachment","status":"current","title":"Path LUN is dead.doc",?version?:{?number?:2},"ancestors":[],"operations":[],"children":[],"descendants":[],"body":[],"restrictions":[],"metadata":{"comment":"Path LUN is dead"}}
```

You may find the version of the attachment from your GET call. You may have to expand the payload from the GET to see the version and increment up from that value. You may find the schema for the GET attachments at [GET /rest/api/content/{id}/child/attachment](https://docs.atlassian.com/ConfluenceServer/rest/6.14.1/#api/content/{id}/child/attachment-getAttachments) (see SCHEMA under responses).

I hope this helps and clarifies why you're getting the STATUS 400 error. Please do let us know if this contributed to resolving the issue.

Regards,  
Stephen Sifers

comments: author: Alex Perez
body: Hi Stephen,

sorry by the late response but I broke my arm and Im just retaking topics ...

I changed the API call as you advised, but is not working,

initially, in my forst message, the call was a PUT with

```
{"id":"att16259572","type":"attachment","status":"current","title":"Path LUN is dead.doc","ancestors":[],"operations":[],"children":[],"descendants":[],"body":[],"restrictions":[],"metadata":{"comment":"Path LUN is dead"}}
```

Now, I changed it to (adding the version / number element)

```
{"id":"att4000440004440","type":"attachment","status":"current","title":"Asipresencia Operational Procedure.doc","version":{"number":4},"ancestors":[],"operations":[],"children":[],"descendants":[],"body":[],"restrictions":[],"metadata":{"comment":"test comment from API"}}
```

And tested also with (added version/number element from the previous GET call, but without incrementing it):

```
{"id":"att4000440004440","type":"attachment","status":"current","title":"Asipresencia Operational Procedure.doc","version":{"number":3},"ancestors":[],"operations":[],"children":[],"descendants":[],"body":[],"restrictions":[],"metadata":{"comment":"test comment from API"}}
```

But im getting a 400 errorcode in every case,

How can I figure out where is the problem with this call?

thank you


author: Stephen Sifers
body: Hello Alex,

Thank you for sending over what your JSON payload is for your PUT call. If you could, so I may better understand, include the GET call for the attachment in question? This will allow me to see what the JSON looks like and how it should be formatted for an update/PUT call.

We look forward to your response to help get your API calls updating as needed.

Regards,  
Stephen Sifers


Tags: api

atl-adm

confluence-development

developer

rest