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 2022-04-22 21:32:15

zrgss
Member
Registered: 2022-04-22
Posts: 7

API Rest bad_array_error

Hey guys!
I'm starting in programming and i'm developing a Rest API using laravel and guzzle. However, when performing the post method to create a Ticket, postman gives that error:


GuzzleHttp\Exception\ClientException: Client error: `POST /apirest.php/ticket` resulted in a `400 Bad Request` response:
["ERROR_BAD_ARRAY","o parâmetro de entrada deve ser um vetor de objetos; ver documentação em seu navegador em https:/ (truncated...)
(This error description is PT-BR language)

This is my code


public function createTicket () {


        $headers = [
            'Content-type' => 'application/json',
            'App-Token' => 'myapptoken',
            'Session-Token' => 'mysessiontoken'
        ];

       

        $data = array(     
                'entities_id' => 0,
                'name' => "Infraestrutura | E-mail",
                'content' => 'Perdi a senha do meu e-mail, teria como recuperar?',
                'type' =>  1,
                'status' => 1,
                'urgency' => 1,
                'impact' => 1,
                'priority' => 1,
                'itilcategories_id' => 0           
           
        );

        $client = new Client ([
            'headers' => $headers,
            'form_params' => $data
        ]);
       
        $response = $client->request('POST','url/apirest.php/ticket',[
            'headers' => [
                $headers
            ],
            'input' => [
                $data,
            ]
           
            ]);

            return $reponse-getBody()->getContents();

}



It my first experience in development REST Api, can someone help me?

OBS:
I removed the session-token and app token data for security reasons, but they are correct.

Offline

#2 2022-04-25 01:15:09

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

Re: API Rest bad_array_error

The input property should be set to $data not:
[
    $data
]

I am not sure that the parameters you set when creating the client are needed either.


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

#3 2022-04-27 22:52:41

zrgss
Member
Registered: 2022-04-22
Posts: 7

Re: API Rest bad_array_error

I solved using this

$params =
        [ 'json' => [
            'input' => [
                'entities_id' => 0,
                'name' => "Infraestrutura | E-mail",
                'content' => 'teste passar requerente?',
                'type' =>  1,
                'status' => 1,
                'urgency' => 1,
                'impact' => 1,
                'priority' => 1,
                'itilcategories_id' => 0,
                '_users_id_requester' => 3878,
                'groups_id' => 13         
            ]
        ]
        ];

        $client = new Client ([
            'headers' => $headers
        ]);

               
       

         $request = $client->request('POST','link/apirest.php/ticket' , $params);

       
       
         $response = $request->getBody()->getContents();

         return json_decode($response);


But I have one more question, how do I assign a ticket created to a group? I tried using 'groups_id' but without success.

Offline

#4 2022-04-28 11:34:02

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

Re: API Rest bad_array_error

to assign to a group  add :
_groups_id_assign


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

Online

#5 2022-06-28 21:47:15

zrgss
Member
Registered: 2022-04-22
Posts: 7

Re: API Rest bad_array_error

Solved using this

$params = [
    //             RequestOptions::JSON => [
    //                 'input' => [
    //                     'entities_id' => ,
    //                     'name' => '',
    //                     'content' => '',
    //                     'type' =>  1,
    //                     'status' => 2,
    //                     'urgency' => 1,
    //                     'impact' => 1,
    //                     'priority' => 3,
    //                     'itilcategories_id' => 0,
    //                     '_users_id_requester' => ,
    //                     '_groups_id_assign' => ,
    //                     '_users_id_assign' => ''
    //                     ]
    //                 ]
    //             ];

Thanks for help!

Offline

Board footer

Powered by FluxBB