Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update method #25

Open
skreu opened this issue Oct 24, 2024 · 1 comment
Open

Update method #25

skreu opened this issue Oct 24, 2024 · 1 comment

Comments

@skreu
Copy link

skreu commented Oct 24, 2024

Hi !
First of all, thank you very much for your work.
I can't sort out why i get an error.
Everything works fine except since I upgraded my GLPI instance.

Useful informations :
glpi-api-0.4.0 (trying to update to 0.5.0 to test)
requests-2.31.0 (updating as well)
GLPI 10.0.16

When i use this :

myinstance.api.update("Appliance", {"id":1, "name": "Myname"}) works beautifully...
if i try the exactly same but with Entity, let's say for example :
myinstance.api.update("Entity", {"id":1, "tag": "homeentity"}) i get a JSONDecodeError.

wasn't doint that before my upgrade (quite old @ GLPI10.0.6)

Response is completely empty, hence the error.
But if i try with curl in the terminal, everything works fine. Can't explain why.

I'm updating the packages today, and come back for more informations.

@fmenabe
Copy link
Member

fmenabe commented Nov 2, 2024

Hello @skreu

I've tested it and I can confirm there is a problem. When calling an update for an Entity, the response is HTTP 200 but there is no data in it (it should return a JSON document). As the code use the json() method on the response from requests.Response on an empty response it crashes with the JSONDecodeError error.

When using curl, it doesn't work either (you get a 200 return code but with no content and the entity is not updated):

curl http://127.0.0.1:8080/apirest.php/Computer -XPUT -H 'Content-Type: application/json' -H 'Session-Token: xxxxx' -H 'App-Token: xxxxxxx' -d '{"input": [{"id": "1", "tag": "test"}]}' -i
HTTP/1.1 200 OK
Content-Length: 0                                                                                                                                                                                                
Content-Type: text/html; charset=UTF-8

From GLPI point of view, I looked at the logs of my test instance, and I have this error when I make a call:

glpiphplog.CRITICAL:   *** Uncaught Exception Error: Attempt to assign property "id" on array in /var/www/html/glpi/src/Api/APIRest.php at line 347
  Backtrace :                      
  apirest.php:57                                     Glpi\Api\APIRest->call()                                                                                                                                    
  public/index.php:82                                require()

And when I comment the PHP code directly in my Docker test instance, it does work:

                    $input = (array) ($this->parameters['input']);
                    //if (
                    //    ($id > 0 || $id == 0 && $itemtype == "Entity")
                    //    && !isset($input['id'])
                    //) {
                    //    $this->parameters['input']->id = $id;
                    //}
                    $response = $this->updateItems($itemtype, $this->parameters);

(it works but for entities other than the root, I get a permission error, but this is probably another thing)
(I don't understand this code as I don't know where $id comes from - I think it is always equals to 0 - and $input is a list of updates. So it just can't work ...)

This seems to be a problem with GLPI and I don't know if I should do something in the library. I could check if the response is empty to prevent the JSONDecodeError, but the call still do nothing (should I raised an error ?). And I don't want to manage every quirks of the GLPI API (which has a lot of weird behaviors).

Let me know what you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants