Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I upload a attachment via REST API, after a deleting it via REST API?

antony terrence
Contributor
April 18, 2019

After I delete an attachment via REST API, when I upload the same attachment via API, the request is successful - I get a 200 response code. However, there is no data in the attachment and it cannot be previewed. Any any idea why this is happening?


def upload_image(file, fb, parentID, url, data, auth, files, headers):
print("hello")
r = requests.post(url, data=data, auth=auth, files=files, headers=headers)
if r==200:
return r
elif r.status_code==400:
print("the image exists. updating the image")
imgid, version = get_attachment_id(file, parentID)
urld = base+imgid
r2 = requests.delete(urld, auth=auth)
r2 = upload_image(file, fb, parentID, url, data, auth, files, headers)
return r2

 

the attachments on the page:

sample_for_question.png

1 answer

0 votes
Diego
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 22, 2019

Hello there!

As I understand, attachment upload is not working as expected with your Confluence instance.Is that correct Antony?

Thanks for sharing part of your code with us! However, we still need to look into the cURL string you are sending to Connie. It should look like this:

curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@myfile.txt" -F "comment=This is my File" http://myhost/rest/api/content/123/child/attachment

If you send us your cURL string, be careful not to disclose any sensitive information. Community is an open space and anyone can see it here!

With the limited access we have to your code, it is hard to determine what might be wrongly converted or returned, if that is the root cause.

You can have a deeper look at our create attachment procedure via REST here:

REST API - Create attachment

 

Looking forward to your reply Antony!

antony terrence
Contributor
April 22, 2019

@Diego  my code for creating the page and uploading images to  the page works fine. I also figured out how to update the attachments. However, I do not want to keep updating the attachments since that creates multiple versions of each attachment. So in my code, I check if a particular attachment exists. If it exists, I delete the attachment and then try to upload the updated attachment. After I delete the attachment, my code for creating the attachment works. However, as shown in the image, the attachment files lose data and cannot be previewed - after deleting the attachment, when I upload the attachment their size is 0.0 kb. 

antony terrence
Contributor
April 23, 2019

@Diego  I kind of solved this with the highlighted line. Thank you for your help.resolved.png

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events