Hello ,
I’ve read these helpful posts (1, 2); 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.
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:
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:
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:
"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!