Uploading attachments using rest api.
Asked by Paula Cristina Vaz:

Hello.

I've trying to upload an attachment to a page with id [857705024](http://my-host.atlassian.net/rest/api/content/857705024/child/attachment) 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


Answers:
author: DPKJ
body: 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
```

comments: author: Paula Cristina Vaz
body: Thanks.

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


author: DPKJ
body: 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
```


author: Paula Cristina Vaz
body: 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. :-(


author: DPKJ
body: 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?
```


author: Paula Cristina Vaz
body: It was the wiki! Thanks a lot! :-D


author: DPKJ
body: [@Paula Cristina Vaz](/t5/user/viewprofilepage/user-id/1780228)You 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.


author: Paula Cristina Vaz
body: No problem. Thank you again. :-)


author: Varun.Ravindranath2
body: Any update [@Paula Cristina Vaz](/t5/user/viewprofilepage/user-id/1780228) 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.


author: PBTesting
body: [@DPKJ](/t5/user/viewprofilepage/user-id/690016) I am trying to upload an image file to issue using vbscript but no luck.


Tags: cloud

confluence-cloud

rest-api