Unable to Associate Category Banner via API During Category Creation with Python Code

Hi All,

We are using a Python script to create categories and subcategories with hierarchy, along with category descriptions, and have successfully accomplished this. However, we also want to associate a category banner image based on input from the CSV file we are using, but we are currently unable to do so, even though the image file gets uploaded successfully to discourse instance and python script is showing as category updated successfully without failure.

  1. Image Upload Successful but Not Associated with Category
  • Title: Why is the uploaded image not appearing as the category banner in Discourse?
  • Content:
    I’m using the Discourse API to upload an image and associate it as a category banner. The Python code reports that the category was successfully updated, and the uploaded image is visible in the Discourse instance. However, the banner image is not applied to the category.
    Here’s the code snippet I’m using:

python

data = {
    "name": category_name,
    "slug": category_slug,
    "uploaded_background": background_image_url
}
response = requests.post(create_url, json=data, headers=headers)
print(response.json())

Could there be an additional step or setting required to associate the image with the category?


  1. Uploaded Image Not Applied to Category Despite Success Message
  • Title: Why does the category banner not update even though the API call is successful?
  • Content:
    I upload an image to the Discourse instance using the API and assign it as the category banner. The API call returns a success response, and the image is visible in the uploads section of Discourse. However, the category banner remains blank.
    Here’s how I’m assigning the banner image:

python


uploaded_image_url = upload_image(image_path)
if uploaded_image_url:
    data = {
        "name": "Sample Category",
        "slug": "sample-category",
        "uploaded_background": uploaded_image_url
    }
    response = requests.post(create_url, json=data, headers=headers)
    print(f"Response: {response.status_code}, {response.json()}")

Why might the banner image not appear on the category despite the API reporting success?


  1. Python Script Reports Success, But Banner Image Not Visible
  • Title: Why is the Discourse category banner image not updating after a successful API response?
  • Content:
    I’m trying to upload and assign a category banner image using the Discourse API. The Python script logs success, and I can see the uploaded image in the Discourse instance. However, the image is not associated with the category.
    Code snippet:

python

response = requests.post(create_url, json=data, headers=headers)
if response.status_code == 200:
    print("Category updated successfully.")
else:
    print(f"Failed to update category: {response.json()}")

Is there a specific parameter or additional step required to ensure the banner image is applied to the category?

By following Reverse engineer the Discourse API your python script should be able to do everything you can do by using UI.

1 Like