# Using Python to upload avatar (Discourse API)

**URL:** <https://meta.discourse.org/t/using-python-to-upload-avatar-discourse-api/219731>\
**Category:** Product Support\
**Created:** [March 1, 2022, 6:59pm UTC](https://meta.discourse.org/t/using-python-to-upload-avatar-discourse-api/219731 "2022-03-01T18:59:50Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Allan\_Campopiano](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/allan_campopiano/32/251599_2.png) [@Allan\_Campopiano](https://meta.discourse.org/u/Allan_Campopiano)\
**Post date:** [March 1, 2022, 6:59pm UTC](https://meta.discourse.org/t/using-python-to-upload-avatar-discourse-api/219731/1 "2022-03-01T18:59:50Z")

</div>

Hello 👋,

I’ve read these helpful posts ([1](https://meta.discourse.org/t/how-to-upload-image-using-discourse-api/148869), [2](https://meta.discourse.org/t/upload-avatar-image-with-api/55253)); however, I’m still missing something that is preventing me from successfully uploading an avatar from Python to Discourse using the API. I am the admin of my Discourse site.

**The good news:** It works in Postman as shown below.

 ![Screen Shot 2022-03-01 at 1.41.40 PM](https://global.discourse-cdn.com/meta/original/3X/4/f/4fae241778aae64a9a7abfd324cd3f7536eef579.jpeg)

**The not so good news:** I haven’t been able to translate this to Python (getting 400 or 422 errors, depending on what I attempt). I’ve tried the following:

```python
img_data = requests.get("my_image_url").content

data={
"type": "avatar",
"user_id": user_id,
"synchronous": True,
"file": img_data
}

url = "<my site>/uploads.json"

response = requests.request("POST", url, headers=headers, data=data)

```

This gives me a 400 error. I’ve also tried using a local image just to see if I could get it to work:

```python
with open('my_local_image.jpg', "rb") as f:
    image_b64 = base64.b64encode(f.read()).decode("utf8")

data={
"type": "avatar",
"user_id": user_id,
"synchronous": True,
"file": image_b64
}

url = "<my site>/uploads.json"

response = requests.request("POST", url, headers=headers, data=data)

```

This gives me a 422 error:

```plaintext
"failed":"FAILED","message":"undefined method `tempfile\' for #\\u003cString:0x00007f5befda7af8\\u003e"

```

🙏 I feel like I am close but I’m somewhat new to using API. Any pointer in the right direction would be much appreciated!

---

_[View the full topic](https://meta.discourse.org/t/using-python-to-upload-avatar-discourse-api/219731)._
