You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2024-04-04 14:05:21

galctco
Member
Registered: 2024-03-13
Posts: 17

[solved]Get associated data of entry through API

Hello, Im trying to create an automation that retrieves all entries of all kind of assets.
When I make a GET request for a specific asset type, for example computers,
I get a json containing all the information regarding computer but for instance in the "Manufacturer" field I get a link to the other table in the db through API.
Is there a way to receive the data back with the actual values, instead of links?

Offline

#2 2024-04-04 14:13:37

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,287

Re: [solved]Get associated data of entry through API

you could add
expand_dropdowns =1
and
add_keys_names=1

this might give you names instead of IDs


Trouver la panne avant de réparer...
GLPI10.0.16 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9

Offline

#3 2024-04-04 14:24:48

galctco
Member
Registered: 2024-03-13
Posts: 17

Re: [solved]Get associated data of entry through API

when using add_keys_names=1 I get empty response.

Offline

#4 2024-04-04 17:39:42

cconard96
Moderator
Registered: 2018-07-31
Posts: 2,809
Website

Re: [solved]Get associated data of entry through API

The "add_keys_names" parameter expects an array of field names. Its usage is frankly confusing, but the pull request that added may shed some light on it:
https://github.com/glpi-project/glpi/pull/6858

For the original question, expand_dropdowns=1 should be enough I think.


GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.

Offline

#5 2024-04-09 09:34:10

galctco
Member
Registered: 2024-03-13
Posts: 17

Re: [solved]Get associated data of entry through API

I could not quite understand how to make it work. maybe you can shed some light on where I'm doing it wrong:

   self.get(type, expand_dropdowns=1, range='0-150', add_keys_names=['ComputerModel', 'ComputerType', 'Manufacturer', 'User']).json()

assuming self.get receieves 'type' === endpoint, and keyworded arguments as parameters.
I've tried

 add_keys_names=['ComputerModel', 'ComputerType', 'Manufacturer', 'User'])

and also

add_keys_names="['ComputerModel', 'ComputerType', 'Manufacturer', 'User']")

but none seems to work.

Offline

#6 2024-04-09 10:40:38

galctco
Member
Registered: 2024-03-13
Posts: 17

Re: [solved]Get associated data of entry through API

also, I'm getting back username, and I'd like to have User full name instead, is that possible?

Offline

#7 2024-04-09 16:39:53

cconard96
Moderator
Registered: 2018-07-31
Posts: 2,809
Website

Re: [solved]Get associated data of entry through API

Probably not. You need to make multiple API requests if you want more information. One request for the asset itself and then separate requests to each associated item you want more info for.
This will be improved with GLPI 11 and the new REST API which has a GraphQL wrapper which lets you ask for data across different "schemas".


GLPI Collaborator and Plugin Developer.
My non-English comments are automated translations. Sorry for any confusion that causes.
Mes commentaires non anglais sont des traductions automatiques. Désolé pour toute confusion qui cause.
Mis comentarios que no están en inglés son traducciones automáticas. Perdón por cualquier confusión que cause.

Offline

#8 2024-04-10 14:02:38

galctco
Member
Registered: 2024-03-13
Posts: 17

Re: [solved]Get associated data of entry through API

I'm trying to fill the relations manually with additional API calls but it seems the href's supplied in the LINKS columns are broken.

for instance:

 {'rel': 'MonitorType', 'href': 'https://localhost/apirest.php/MonitorType/1'}

// but when making request to

https://<actual-ip>/apirest.php/MonitorType/1

// I get back this error:

"ERROR_RESOURCE_NOT_FOUND_NOR_COMMONDBTM","resource not found or not an instance of CommonDBTM; view documentation in your browser at https://localhost/apirest.php/#ERROR_RESOURCE_NOT_FOUND_NOR_COMMONDBTM"

// I've looked into the error docs @ <relevant-ip>/glpi/apirest.php but it seems ERROR_RESOURCE_NOT_FOUND_NOR_COMMONDBTM is not documented.

any inputs from you on this one?

Last edited by galctco (2024-04-10 14:03:02)

Offline

#9 2024-04-10 14:19:02

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,287

Re: [solved]Get associated data of entry through API

glpi 10.0.14 :
On my instance this url

https://Myglpiserver/apirest.php/MonitorType/13/

   returns
 

{"date_creation":null,"date_mod":null,"id":13,"name":"LCD 20\"","comment":""}


also


https://myglpiserver/apirest.php/Monitor/8725/?expand_dropdowns=1

returns

{"is_recursive":0,
"date_creation":null,
"id":8725,
...
,"monitortypes_id":"LCD 20",
...
,"links":[ ...{"rel":"MonitorType","href":"https://Myglpiserver/apirest.php/MonitorType/13"}...}


Trouver la panne avant de réparer...
GLPI10.0.16 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.3 reports 1.16.0 formcreator 2.13.9, datainjection 2.13.5 fields 1.21.9

Offline

#10 2024-04-10 15:52:23

galctco
Member
Registered: 2024-03-13
Posts: 17

Re: [solved]Get associated data of entry through API

Bug found in my code. thank you for your support  gentlemen. carry on and lock this thread (writes with shame...)

Offline

Board footer

Powered by FluxBB