# Using Python to upload avatar (Discourse API)

**URL:** https://meta.discourse.org/t/using-python-to-upload-avatar-discourse-api/219731
**Category:** 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:** 4
**Page:** 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!

---

<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, 7:15pm UTC](https://meta.discourse.org/t/using-python-to-upload-avatar-discourse-api/219731/2 "2022-03-01T19:15:36Z")

</div>

Ooo! I discovered from [here](https://meta.discourse.org/t/uploading-a-local-image-using-the-api/127071) that I can use “url” as a parameter as follows:

```python
data={
"type": "avatar",
"user_id": user_id,
"synchronous": True,
"url": "url_to_my_image"
}

url = "<my_site>/uploads.json"

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

```

I finally get a 200!

Now, I know there is a 2nd step I need to do to actually complete this process 🤔, let me see what I can do…

---

<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, 7:31pm UTC](https://meta.discourse.org/t/using-python-to-upload-avatar-discourse-api/219731/3 "2022-03-01T19:31:01Z")

</div>

Yes! I figured out the 2nd step!

```python
url = "<my_site>/users/<username>/preferences/avatar/pick.json"

data={
"upload_id": '<id_from_the_1st_step>',
"type": 'uploaded'
}

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

```

Love it! ❤

---

<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: [March 31, 2022, 7:31pm UTC](https://meta.discourse.org/t/using-python-to-upload-avatar-discourse-api/219731/4 "2022-03-31T19:31:05Z")

</div>

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