How to Upload / Change user profile pic with API in Python

After checking all posts, I tried all solutions, but none of them worked.
Can anyone help me?

1 Like

Welcome, Muskan!

Are you trying to use the Discourse API to change a user’s profile pic?

You’ll need to say more about what you tried to do, including the code that you expected to work and what happened when you ran it.

Uploading an image via the API is a bit tricky, so you’ll want to make sure that you know how to do something easier first (like change the user’s name or location).

3 Likes

Thank You for the reply!

I already created 500+ activated users with API

here is the code I am using for image upload

headers1 = {
‘Api-Key’: key,
‘Api-Username’: username,
}
headers2 = {
‘Api-Key’: key,
‘Api-Username’: username,
‘Content-Type’: ‘application/json;’,
}
response1 = requests.post(‘https://domain.com/uploads.json’,
files = {‘files’: (‘image.png’, open(f’{file_path}’,‘rb’), ‘image/png’)},
data={‘type’:‘avatar’,‘user_id’:9 ,‘synchronous’: ‘true’},
headers=headers1 )

getting upload_id from response

response2 = requests.post(f’https://domain.com/users/{Api_Username}/preferences/avatar/pick’,
headers=headers2,
data={‘upload_id’:3,‘type’:‘uploaded’})

gives 404

@pfaffman Please check this code and guide me