Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Uploading attachments using rest api.

Paula Cristina Vaz August 26, 2019

Hello.

I've trying to upload an attachment to a page with id 857705024 using the rest api, but I keep receiving a 301.

Can anyone help me? :-)

Here is the command:

curl -D- -u user:api_key -X POST -H "X-Atlassian-Token: nocheck"  -F "file=@image.png" -F "comment=myImage" http://my-host.atlassian.net/rest/api/content/857705024/child/attachment

 

TIA

Paula

2 answers

1 accepted

0 votes
Answer accepted
DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 26, 2019

HTTP Response Code 301 implies "Moved Permanently"

So in order to follow this redirection we need to pass `-L` flag to curl request. So updated command will look like.

curl -L -D- -u user:api_key -X POST -H "X-Atlassian-Token: nocheck"  -F "file=@image.png" -F "comment=myImage" http://my-host.atlassian.net/rest/api/content/857705024/child/attachment
Paula Cristina Vaz August 26, 2019

Thanks.

Now I get a 404, but the number of the page is correct.

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 26, 2019

Oh! I noticed,

In place of POST, you need use PUT method for this api.

curl -L -D- -u user:api_key -X PUT -H "X-Atlassian-Token: nocheck"  -F "file=@image.png" -F "comment=myImage" http://my-host.atlassian.net/rest/api/content/857705024/child/attachment
Paula Cristina Vaz August 27, 2019

Hi DPK J.

Thanks for your help, but with POST or PUT the result is the same: a 404.

I even tried with a text file, but the result is always the same.

 

I don't seem to be able to upload attachments to our confluence. :-(

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 27, 2019

As per API documentation, these are the reason for 404 response,

  • The requested content is not found.
  • The user does not have permission to view it
  • The attachment exceeds the maximum configured attachment size.

Have you checked all these?

Try this, as you are using confluence api, we need to context path 'wiki' also. And method will be post.

curl -D- \
  -u <EMAIL>:<API_TOKEN> \
  -X POST \
  -H "X-Atlassian-Token: nocheck" \
  -F "file=@example.txt" \
  -F "minorEdit=true" \
  -F "comment=Example attachment comment" \
  http://<host>.atlassian.net/wiki/rest/api/content/857705024/child/attachment 
Paula Cristina Vaz August 27, 2019

It was the wiki! Thanks a lot! :-D

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 27, 2019

@Paula Cristina VazYou are welcome, I am sorry that I wasn't able to figure out that in first place.

I am using and developing for both server and cloud versions, so many a times I get confused with APIs.

Paula Cristina Vaz August 27, 2019

No problem. Thank you again. :-)

Varun.Ravindranath2 August 1, 2023

Any update @Paula Cristina Vaz  Did you ever solved this? Looks like it is returning 404 when we try as per the documentation. 

I think i figured it out -- if somebody reach out this query - here is this fix.

404 happens since you have not got url correctly.

https://{host}.atlassian.net/wiki/rest/api/content/{id}/child/attachment.

 

When i tried this from postman it worked with the token and required information. It should also work with curl.

 

curl -L -D- \
-u admin:admin \
-X POST \
-H 'X-Atlassian-Token: no-check' \
-H 'Accept: application/json' \
-F 'file=@"attachement.txt"' \
-F 'minorEdit="true"' \
-F 'comment="Example attachment comment"; type=text/plain; charset=utf-8' \
'https://{host}.atlassian.net/wiki/rest/api/content/{id}/child/attachment'

 

Thanks in advance.

0 votes
PBTesting November 7, 2019

@DPKJ  I am trying to upload an image file to issue using vbscript but no luck. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events