Hi
here is bashscript that upload file to confluence, first time it upload correctly second time return error because of file with same name exist. is there any way like we upload file with same name through the web UI and it will add new version of file, do the same thing with curl command?
Here is my bashscript:
curl -u $USER_NAME:$API_TOKEN \
-X POST \
-H "X-Atlassian-Token: nocheck" -F "file=@${ATTACHMENT_FILE_NAME}" -F "comment=File attached via REST API" \
${CONFLUENCE_BASE_URL}/rest/api/content/${PAGE_ID}/child/attachment
here is the error:
{"statusCode":400,"data":{"authorized":false,"valid":true,"allowedInReadOnlyMode":true,"errors":[],"successful":false},"message":"Cannot add a new attachment with same file name as an existing attachment: result.csv","reason":"Bad Request"}
Any idea?
Thanks
Hi @kohopir975 ,
To upload a new version of an attachment with the same name in Confluence using the REST API, you can follow these steps:
1. Retrieve the existing attachment ID:
Before uploading a new version of the attachment, you need to know the ID of the existing attachment. You can use the Confluence REST API to retrieve this information. Here's an example command:
curl -u $USER_NAME:$API_TOKEN \
-X GET \
${CONFLUENCE_BASE_URL}/rest/api/content/${PAGE_ID}/child/attachment?filename=${ATTACHMENT_FILE_NAME}
Replace `$USER_NAME`, `$API_TOKEN`, `${CONFLUENCE_BASE_URL}`, `${PAGE_ID}`, and `${ATTACHMENT_FILE_NAME}` with the appropriate values.
This command will return the JSON response containing the details of the existing attachment, including its ID.
2. Upload the new version of the attachment:
Once you have the existing attachment ID, you can upload the new version using the Confluence REST API. Here's an example command:
curl -u $USER_NAME:$API_TOKEN \
-X POST \
-H "X-Atlassian-Token: nocheck" \
-F "file=@${NEW_ATTACHMENT_FILE_NAME}" \
-F "minorEdit=true" \
${CONFLUENCE_BASE_URL}/rest/api/content/${PAGE_ID}/child/attachment/${EXISTING_ATTACHMENT_ID}/data
Replace `$USER_NAME`, `$API_TOKEN`, `${CONFLUENCE_BASE_URL}`, `${PAGE_ID}`, `${NEW_ATTACHMENT_FILE_NAME}`, and `${EXISTING_ATTACHMENT_ID}` with the appropriate values.
This command uploads the new file using the same name and specifies the `minorEdit` parameter as `true` to indicate that it's a minor edit.
By following these steps, you can upload a new version of an attachment with the same name in Confluence using the REST API.
/Markus
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @kohopir975
since I was just working on the same, I'd like to share my findings:
In your original code you used the method POST, which is exclusively for creating new attachments. If there already is an attachment with the same filename you're trying to upload, then it throws the reported error.
When you use PUT instead, then it checks if the file already exists and either creates a new or updates the version of an existing file with that filename.
API documentation for the PUT method: https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-content---attachments/#api-wiki-rest-api-content-id-child-attachment-put
Compared with the other solution you don't have to identify the attachment-id first, so you can use it flexibly, no matter whether the page already contains this attachment or not.
Hope it still helps 🙂
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
When I use PUT, I get 405 Method not allowed back. POST to create the first attachment works without problems. The workaround from @Markus Fredén also works.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Oliver
I'm sorry that it doesn't work for you, for me it does. It's hard to troubleshoot without knowing your code.
But anyway, if the workaround is suitable for you, then it's alright I guess :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.