kgori_dev
kgori_dev

Reputation: 166

How do i send a PUT request in Laravel

I am trying to update data through sending an http put request to ServiceDesk plus api. When using the console that comes with the system, it works well but when I try to send a request to the same api from Laravel it does not work.

request from the console below

enter image description here

I am trying to send a request to the same url using the code below.

 private function openTicket($notification)
 {
    $data = json_encode(['input_data' => ['request' => ['subject' => $notification->subject,
            'description' => $notification->description,
            'status' => ['name' => 'Open']]]]);


    $request_id = $notification->request_id;
    $response = Http::withHeaders([
            'technician_key' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX',
            'Accept' => 'application/json'
    ])->put('http://localhost:8082/api/v3/requests/' . $request_id, $data);

     dd($response);
    }

and im getting an error 400 bad request.

enter image description here

Upvotes: 2

Views: 1650

Answers (2)

kgori_dev
kgori_dev

Reputation: 166

I managed to find a solution and it is as follows,

private function openTicket($notification): bool
    {
        $data = json_encode(['request' => ['subject' => $notification->subject,
            'description' => $notification->description,
            'status' => ['name' => 'Open']]]);

        $request_id = $notification->request_id;
        $response = Http::withHeaders([
            'technician_key' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
            'Content-Type' => 'application/x-www-form-urlencoded'
        //added asForm() before put
        ])->asForm()->put('http://localhost:8082/api/v3/requests/' . $request_id, [
            'input_data' => $data
        ]);
      
        if ($response->status() == 200) {
            return true;
        }
        return false;
    }

I added asForm() before the put function. This is because asForm() indicates that the request contains form parameters. I also modified the $data object from

$data = json_encode(['input_data' => ['request' => ['subject' => $notification->subject,
            'description' => $notification->description,
            'status' => ['name' => 'Open']]]]);

to

$data = json_encode(['request' => ['subject' => $notification->subject,
            'description' => $notification->description,
            'status' => ['name' => 'Open']]]);

Then it worked as i had expected.

Upvotes: 0

gbalduzzi
gbalduzzi

Reputation: 10176

You should not do json_encode, laravel Http module will automatically do it for you. I think your data is json_encoded twice right now.

$data = [
  'input_data' => [
    'request' => [
      'subject' => $notification->subject,
      'description' => $notification->description,
      'status' => ['name' => 'Open']
    ]
  ]
]);


    $request_id = $notification->request_id;
    $response = Http::withHeaders([
            'technician_key' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX',
            'Accept' => 'application/json'
    ])->put('http://localhost:8082/api/v3/requests/' . $request_id, $data);

     dd($response);

I just noticed. From the documentation you provided in the screenshot, the input_data nesting level in the array should not exist

$data = [
  'request' => [
    'subject' => $notification->subject,
    'description' => $notification->description,
    'status' => ['name' => 'Open']
  ]
]);

Upvotes: 2

Related Questions