Unable to load an image in a category with API in Python #1979

Closed
opened 2026-10-05 01:03:00 +02:00 by adam · 2 comments
Owner

Here is a minimal sample code. It results in an error:
Erreur : 405 {"stat":"fail","err":405,"message":"The image (file) is missing"}
A session is created successfully. Then we try to send an image after a check that the path is OK for the image. But it fails.

import requests
import configPiwigo as cp
piwigo_base_url = "https://galleries.grains-de-culture.fr/ws.php"
url = piwigo_base_url+"?format=json&method=pwg.session.login"
auth_data = {
    "username": cp.configPiwigo["login"],
    "password": cp.configPiwigo["pass"],
}
session = requests.Session()  # Crée une session persistante
response = session.post(url, data=auth_data)

if response.ok and response.json().get("stat") == "ok":
    print("Connexion réussie!")
    image_path = "image.jpg"
    # check if the image is findable
    with open(image_path,"r") as fimage:
        print("image trouvée: ", image_path)

    # send the image
    payload = {
        "image": image_path,
        "method": "pwg.images.addSimple",
        "category": 15,
        "name": "Test",
    }

    # Envoi de la requête avec authentification
    response_image = session.post(
        piwigo_base_url+"?format=json&method=pwg.images.addSimple",
        data=payload
    )

    # Vérification du succès de l'opération
    if response_image.status_code == 200:
        print("image uploaded in my piwigo, category 15")
    else:
        print("Erreur :", response_image.status_code, response_image.text)

Here is a minimal sample code. It results in an error: Erreur : 405 {"stat":"fail","err":405,"message":"The image (file) is missing"} A session is created successfully. Then we try to send an image after a check that the path is OK for the image. But it fails. ``` import requests import configPiwigo as cp piwigo_base_url = "https://galleries.grains-de-culture.fr/ws.php" url = piwigo_base_url+"?format=json&method=pwg.session.login" auth_data = { "username": cp.configPiwigo["login"], "password": cp.configPiwigo["pass"], } session = requests.Session() # Crée une session persistante response = session.post(url, data=auth_data) if response.ok and response.json().get("stat") == "ok": print("Connexion réussie!") image_path = "image.jpg" # check if the image is findable with open(image_path,"r") as fimage: print("image trouvée: ", image_path) # send the image payload = { "image": image_path, "method": "pwg.images.addSimple", "category": 15, "name": "Test", } # Envoi de la requête avec authentification response_image = session.post( piwigo_base_url+"?format=json&method=pwg.images.addSimple", data=payload ) # Vérification du succès de l'opération if response_image.status_code == 200: print("image uploaded in my piwigo, category 15") else: print("Erreur :", response_image.status_code, response_image.text) ``` <!-- github-sync-id:2655911779 -->
adam closed this issue 2026-10-05 01:03:00 +02:00
Author
Owner

In the documentation https://github.com/Piwigo/Piwigo/wiki/pwg.images.addSimple, the image parameter seems to be a parameter like the others and must contain a path to a file. In reality, it must be the image data in the post request query attachment.

In the documentation https://github.com/Piwigo/Piwigo/wiki/pwg.images.addSimple, the image parameter seems to be a parameter like the others and must contain a path to a file. In reality, it must be the image data in the post request query attachment. <!-- github-comment-id:2476484622 -->
Author
Owner

This can be illustrated with the folllowing extract

    with open(image_path, 'rb') as img_file:
        files = {'image': (image_path, img_file, 'image/jpeg')}
        response = session.post(
            piwigo_base_url+"?format=json&method=pwg.images.addSimple",
            data=payload,
            files=files
        )

This can be illustrated with the folllowing extract ``` with open(image_path, 'rb') as img_file: files = {'image': (image_path, img_file, 'image/jpeg')} response = session.post( piwigo_base_url+"?format=json&method=pwg.images.addSimple", data=payload, files=files ) ``` <!-- github-comment-id:2477402259 -->
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: adam/Piwigo#1979