# Discourse API Uploads

**URL:** https://meta.discourse.org/t/discourse-api-uploads/134545
**Category:** Development
**Tags:** rest-api
**Created:** [November 27, 2019, 2:00pm UTC](https://meta.discourse.org/t/discourse-api-uploads/134545 "2019-11-27T14:00:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Robert\_Work](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@Robert\_Work](https://meta.discourse.org/u/Robert_Work)
#### Post date: [November 27, 2019, 2:00pm UTC](https://meta.discourse.org/t/discourse-api-uploads/134545/1 "2019-11-27T14:00:55Z")

</div>

Hello!

I have a question about how I can using Python3 upload a file. I tried to do something like that, but I still get error.

```plaintext
> import requests
headers = {
    'content-type': 'multipart/form-data', #application/x-www-form-urlencoded ??
    'Api-Key': 'MyWorkingKey',
    'Api-Username': 'discourseAdmin',
}
URL = "https://discourse.com/uploads.json"
content = open('FULL_PATH_TO/avatar.jpg', 'rb').read()
data={"type": "avatar",'files[]': ('avatar.jpg', content, 'image/jpeg')}
requests.post(URL, headers=headers, data=data)

*Status Code: 422*
*Response TExt: '{"failed":"FAILED","message":"undefined method `tempfile\' for \\"avatar.jpg\\":String"}'*

```

I did upload the file via this curl:

```
curl -X POST https://discourse.com/uploads.json \
-H "content-type: multipart/form-data;" \
-H "Api-Key: MyWorkingKey" \
-H "Api-Username: discourseAdmin" \
-F "type=avatar" \
-F "files[]=@FULL_PATH_TO/avatar.jpg"

```

Unfortunately, I did not successfully move this curl command into Python.

---

<div class="post-metadata">

### Author: ![negaduck](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/negaduck/32/171600_2.png) [@negaduck](https://meta.discourse.org/u/negaduck)
#### Post date: [March 9, 2020, 11:20pm UTC](https://meta.discourse.org/t/discourse-api-uploads/134545/2 "2020-03-09T23:20:21Z")

</div>

This worked for me:

```
r = requests.post(f'{rooturl}/uploads.json',
                  files = {'files[]': (file, open(file, 'rb'), 'image/png')},
                  data={'type':'image'},
                  headers={
                      "Api-Username" : sys.argv[1],
                      "Api-Key" : sys.argv[2],
                  })

```

Probably, `content-type': 'multipart/form-data'` should be omitted so the `requests` library can figure out what to.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [April 8, 2020, 11:20pm UTC](https://meta.discourse.org/t/discourse-api-uploads/134545/3 "2020-04-08T23:20:35Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
