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 2018-12-07 17:27:47

ernieek
Member
Registered: 2018-12-07
Posts: 4

Use API to update field but it's not updating but adding

hello,


I use the following powershell code:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Content-Type','Application/Json')
$headers.Add('Session-Token', $GLPI_Session)
$headers.Add('App-Token', $GLPI_Apikey)

$json = '{"input": { "id":71 , "comment": "test"}}'
$result = Invoke-WebRequest -Uri ($GLPI_ApiUrl+'/Computer') -Body $json -ContentType "application/json" -Headers $headers -Method POST


What I will do is trying to update the comment field, and after if this is working the purch date, warranty expiration.

But it's now not updating the computer with id 71, it is adding a new compiter with an other id.....


Can someone help me how to update otherfiels then comment and on the right computer.

Kind regards Erwin

Offline

#2 2018-12-10 23:54:47

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,290

Re: Use API to update field but it's not updating but adding

to update use PUT method, ( not POST)


PS : i move to APIREST Section


Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.3  en test (ubuntu 18.04 mariadb 10.2.4)

Offline

#3 2018-12-11 12:39:52

ernieek
Member
Registered: 2018-12-07
Posts: 4

Re: Use API to update field but it's not updating but adding

Many Thanks, you're my hero smile it works indeed.

One question more, how to findthe field name's? for example i will put some text in the 'Warranty information' but how is the field called?

so i can update it with the api.

Last edited by ernieek (2018-12-11 12:40:25)

Offline

#4 2018-12-11 15:11:43

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,290

Re: Use API to update field but it's not updating but adding

there are diffrent ways :

1) with API :
  /listSearchOptions/Infocom/

returns fields with indexes


[7] => Array (
[name] => Informations sur la garantie
[table] => glpi_infocoms [field] => warranty_info
[datatype] => string
[nosearch] =>
[nodisplay] =>
[available_searchtypes] => Array ( [0] => contains )
[uid] => Infocom.warranty_info )
...

2)look for searchOptions in inc/infocoms.class.php


      $tab[] = [
         'id'                 => '7',
         'table'              => $this->getTable(),
         'field'              => 'warranty_info',
         'name'               => __('Warranty information'),
         'datatype'           => 'string'
      ];


Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.3  en test (ubuntu 18.04 mariadb 10.2.4)

Offline

#5 2018-12-11 15:21:32

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,290

Re: Use API to update field but it's not updating but adding

you can also search option from computer :

  /listSearchOptions/Computer/
...
[52] => Array ( [name] => Informations sur la garantie
[table] => glpi_infocoms
[field] => warranty_info
[datatype] => string
[nosearch] =>
[nodisplay] =>
[available_searchtypes] => Array ( [0] => contains )
[uid] => Computer.Infocom.warranty_info )
or with GLPI Graphic interface look for warranty information and you'll get URL :

glpi/front/computer.php?is_deleted=0&criteria[0][field]=52&criteria[0][searchtype]=contains&criteria[0][value]=dummy

then for computer field id is 52


Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.3  en test (ubuntu 18.04 mariadb 10.2.4)

Offline

#6 2018-12-11 16:01:03

ernieek
Member
Registered: 2018-12-07
Posts: 4

Re: Use API to update field but it's not updating but adding

many thanks smile

$json = '{"input": { "id":71 , "comment": "test comment" , "warranty_info": "test info", "buy_date": "2015-01-01", "end_warranty": "2019-01-01", "warranty_duration": "12"}}'

should work then, but isn't sad

Comment is working but the other fields are not changed....

Offline

#7 2018-12-11 16:05:18

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,290

Re: Use API to update field but it's not updating but adding

are you updating computer or infocom ? ( please give URL)


Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.3  en test (ubuntu 18.04 mariadb 10.2.4)

Offline

#8 2018-12-11 16:13:15

ernieek
Member
Registered: 2018-12-07
Posts: 4

Re: Use API to update field but it's not updating but adding

http://hostname/glpi/apirest.php/Computer

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Content-Type','Application/Json')
$headers.Add('Session-Token', $GLPI_Session)
$headers.Add('App-Token', $GLPI_Apikey)

$json = '{"input": { "id":71 , "comment": "test comment" }}'
$result = Invoke-WebRequest -Uri ($GLPI_ApiUrl+'/Computer') -Body $json -ContentType "application/json" -Headers $headers -Method PUT

$json = '{"input": { "id":71 , "warranty_info": "test info", "buy_date": "2015-01-01", "end_warranty": "2019-01-01", "warranty_duration": "12"}}'
$result = Invoke-WebRequest -Uri ($GLPI_ApiUrl+'/infocom') -Body $json -ContentType "application/json" -Headers $headers -Method PUT

Last edited by ernieek (2018-12-11 16:14:45)

Offline

#9 Yesterday 21:19:22

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 4,290

Re: Use API to update field but it's not updating but adding

1) you should GET     /Computer/71/Infocom/

2) from response extract $InfocomID ( witch should be different from 71)
3)

$json = '{"input": { "id": $InfocomID , "warranty_info": "test info", "buy_date": "2015-01-01", "end_warranty": "2019-01-01", "warranty_duration": "12"}}'
PUT /infocom/$infocomID/


Trouver la panne avant de réparer...
*GLPI 9.1.6+fusion9.1+1.1+behaviours1.5.0+reports+fields+appliances+pdf+badges+formcreator2.5.2 PHP7.0 Mariadb10
*GLPI 9.2.4(behaviours1.5.2+fusion9.2+1.0+applicatifs2.3.0+dashboard 0.8.9)hebergé sur serveur mutualisé.
*GLPI 9.3.3  en test (ubuntu 18.04 mariadb 10.2.4)

Offline

Board footer

Powered by FluxBB