# User avatar selection through API no longer working

**URL:** https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661
**Category:** Development
**Tags:** rest-api
**Created:** [December 9, 2021, 8:59pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661 "2021-12-09T20:59:16Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Spac3Rat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spac3rat/32/167772_2.png) [@Spac3Rat](https://meta.discourse.org/u/Spac3Rat)
#### Post date: [December 9, 2021, 8:59pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/1 "2021-12-09T20:59:16Z")

</div>

Hi everyone. I am using SSO on my forum and avatars are controlled by my website.

Until a few days ago, avatar upload/update from the website to the Discource, via API was working. I now get a 422 - Unprocessable Entity error.

I’ve tried to debug the issue, did some testing with Postman and I have the same issues. The request I am doing is below (with url, username and api\_key removed, of course).

Does any of you know if there’s any issue with this portion of Discourse?

Thank you in advance.

My example:

```plaintext
curl --location --request PUT 'https://{{URL}}/u/{{USERNAME}}/preferences/avatar/pick' \
--header 'Api-Key: {{API_KEY}}' \
--header 'Api-Username: system' \
--header 'Content-Type: application/json' \
--data-raw '{
"upload_id": 972,
"type": "uploaded"
}'

```

---

<div class="post-metadata">

### Author: ![Spac3Rat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spac3rat/32/167772_2.png) [@Spac3Rat](https://meta.discourse.org/u/Spac3Rat)
#### Post date: [December 12, 2021, 8:53pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/2 "2021-12-12T20:53:30Z")

</div>

Anyone? Isn’t there anyone that has found the same problem?

---

<div class="post-metadata">

### Author: ![mattdm](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mattdm/32/216484_2.png) [@mattdm](https://meta.discourse.org/u/mattdm)
#### Post date: [December 13, 2021, 11:07am UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/3 "2021-12-13T11:07:11Z")

</div>

This is probably related to the change to the API to do direct uploads to S3?

---

<div class="post-metadata">

### Author: ![Spac3Rat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spac3rat/32/167772_2.png) [@Spac3Rat](https://meta.discourse.org/u/Spac3Rat)
#### Post date: [December 13, 2021, 11:19am UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/4 "2021-12-13T11:19:10Z")

</div>

I think that would interfere with the actual upload, not the assignment of the avatar to the user.

I can upload the file without any issue. When I make the call to the API to indicate which file to use as an avatar, that’s where I get the error and that started out of the blue.

---

<div class="post-metadata">

### Author: ![Spac3Rat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spac3rat/32/167772_2.png) [@Spac3Rat](https://meta.discourse.org/u/Spac3Rat)
#### Post date: [December 17, 2021, 5:46pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/5 "2021-12-17T17:46:27Z")

</div>

I’m trying my luck and boost this one last time. It’s odd that no one has found this. I am sure it’s not a coding issue since it has been working for over a couple of years now.

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [December 17, 2021, 10:33pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/6 "2021-12-17T22:33:04Z")

</div>

I think this is due to the `discourse connect overrides avatar` site setting.

 ![image](https://global.discourse-cdn.com/meta/original/3X/e/1/e17e2e34c5eb6623b4313bab9a07a673e19bd593.png)

> Overrides user avatar with value from [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) payload. If enabled, users will not be allowed to upload avatars on Discourse.

On my local with that unchecked I get a 200 http response when updating the avatar via the api:

```plaintext
curl -i -sS -X PUT "http://localhost:4200/u/10614bb2d4eacd328c45/preferences/avatar/pick.json" \
-H "Content-Type: multipart/form-data" \
-H "Api-Key: 6cea489d21282803c446fd2e9d236901c3d186f36079911833db4b57c43c01d5" \
-H "Api-Username: blake.erickson" \
-F "upload_id=57" \
-F "type=uploaded"

HTTP/1.1 200 OK

```

And I get a 422 with that setting checked:

```plaintext
curl -i -sS -X PUT "http://localhost:4200/u/021ca796a01ad178bc52/preferences/avatar/pick.json" \
-H "Content-Type: multipart/form-data" \
-H "Api-Key: 6cea489d21282803c446fd2e9d236901c3d186f36079911833db4b57c43c01d5" \
-H "Api-Username: blake.erickson" \
-F "upload_id=57" \
-F "type=uploaded"

HTTP/1.1 422 Unprocessable Entity

```

---

<div class="post-metadata">

### Author: ![Spac3Rat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spac3rat/32/167772_2.png) [@Spac3Rat](https://meta.discourse.org/u/Spac3Rat)
#### Post date: [December 18, 2021, 12:37am UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/8 "2021-12-18T00:37:07Z")

</div>

Hi Blake. Thanks for pitching in.

Enable or disabling it makes no difference, unfortunately.

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [December 20, 2021, 9:28pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/10 "2021-12-20T21:28:40Z")

</div>

I don’t have an answer yet, but just wanted to let you know that this is on my list to look into. I have a sso setup I can test locally, so that our settings match. It seems like we should be respecting that site setting which could be a change someone made recently, but maybe we could add an API override.

---

<div class="post-metadata">

### Author: ![Spac3Rat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spac3rat/32/167772_2.png) [@Spac3Rat](https://meta.discourse.org/u/Spac3Rat)
#### Post date: [December 20, 2021, 9:40pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/11 "2021-12-20T21:40:07Z")

</div>

Thank you very much, Blake. Please let me know if there’s anything I can help with.

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [December 20, 2021, 10:32pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/12 "2021-12-20T22:32:36Z")

</div>

Another reason for a 422 from that method can be if the `allow_uploaded_avatars` setting is false. I bet that’s the issue.

---

<div class="post-metadata">

### Author: ![Spac3Rat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spac3rat/32/167772_2.png) [@Spac3Rat](https://meta.discourse.org/u/Spac3Rat)
#### Post date: [December 20, 2021, 10:57pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/13 "2021-12-20T22:57:01Z")

</div>

Hi Richard! Thanks for your input.

I thought about that as well but it didn’t work. Also, this issue appeared out of the blue. No one changed anything (I am the only admin so there’s no chance someone could have switched any settings), no code changes on the main website, nothing.

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [December 21, 2021, 10:52pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/14 "2021-12-21T22:52:45Z")

</div>

Can you let me know what you have set for `allow_uploaded_avatars`? It no longer is just a true/false setting, but is set to a certain trust level. And can you let me know what the trust level is of the user trying to change their avatar? And are you on the latest version of Discourse?

Here is the code for picking an avatar and the lines that return 422 responses.

 ![image](https://global.discourse-cdn.com/meta/original/3X/f/e/fe19ce4a528de52f4863dcbeb8bc723756f51a7e.png)

Not that it couldn’t be something else deep in the codebase, but it likely is one of these 3. The first one has to do with `discourse_connect_overrides_avatar` and apparently we ruled that one out. I don’t think it is the second one because your curl command looks correct and includes the “uploaded” type. It could possibly still be the third one with the `allow_uploaded_avatars` setting which is why I’d like to know what you have that one set to.

---

<div class="post-metadata">

### Author: ![Spac3Rat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spac3rat/32/167772_2.png) [@Spac3Rat](https://meta.discourse.org/u/Spac3Rat)
#### Post date: [December 22, 2021, 12:00am UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/15 "2021-12-22T00:00:44Z")

</div>

I had it disabled until this problem started. Then I changed it to 0:new user.

But having it disabled always worked for me. I don’t want users to actually upload from the forum, but rather from the website which uses SSO. Still, changing it to 0:new user doesn’t change anything. I still get the same error ☹

 ![image](https://global.discourse-cdn.com/meta/original/3X/7/3/7380a81b8bc12b402ec829f8eb411981bc86d0bf.png)

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [December 22, 2021, 12:52am UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/16 "2021-12-22T00:52:39Z")

</div>

I’m not able to find any recent update that would have stopped avatars to work via the api when all the site settings have them blocked. Regardless, if you are using SSO (or [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true)), you should be using the `/admin/users/sync_sso` api route to update the users avatar not the UI route (`/u/username/preferences/avatar/pick`).

And pass in these params in the request body:

```plaintext
avatar_url: "url-of-image",
avatar_force_update: "true"

```

---

<div class="post-metadata">

### Author: ![Spac3Rat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spac3rat/32/167772_2.png) [@Spac3Rat](https://meta.discourse.org/u/Spac3Rat)
#### Post date: [December 22, 2021, 9:18am UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/17 "2021-12-22T09:18:07Z")

</div>

Hi Blake. Thank you very much for your continued help on this matter.

I can’t find any information about that endpoint in the API documentation. Is that something new?

Also, like I indicated before, avatar update was working fine for several months using the `/u/username/preferences/avatar/pick` endpoint, which is really odd. It just stopped working. That really puzzles me.

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [December 22, 2021, 2:58pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/18 "2021-12-22T14:58:38Z")

</div>

Ya, it really should be in the docs, its not new though, its just different than all the other endpoints.

You can find some info here: [Setup DiscourseConnect - Official Single-Sign-On for Discourse (sso)](https://meta.discourse.org/t/discourseconnect-official-single-sign-on-for-discourse-sso/13045)

And also how the discourse\_api ruby gem uses the `sync_sso` endpoint: [discourse\_api/lib/discourse\_api/single\_sign\_on.rb at main · discourse/discourse\_api · GitHub](https://github.com/discourse/discourse_api/blob/main/lib/discourse_api/single_sign_on.rb) and [discourse\_api/lib/discourse\_api/api/sso.rb at main · discourse/discourse\_api · GitHub](https://github.com/discourse/discourse_api/blob/main/lib/discourse_api/api/sso.rb)

It will need to use the same sso secret that your sso provider is using.

---

<div class="post-metadata">

### Author: ![Spac3Rat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spac3rat/32/167772_2.png) [@Spac3Rat](https://meta.discourse.org/u/Spac3Rat)
#### Post date: [December 22, 2021, 3:21pm UTC](https://meta.discourse.org/t/user-avatar-selection-through-api-no-longer-working/211661/19 "2021-12-22T15:21:54Z")

</div>

Thanks, Blake.

It still doesn’t make sense to me that things just stopped working on their own and I get 422 errors left and right, even with that endpoint.

I’ll try to find a different solution.

Thank you very much for your time.
