Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

teeusantos's avatar

Failed to load resource: the server responded with a status of 500 (Internal Server Error) and The GET method is not supported for route chat. Supported methods: POST. OpenIA / LARAVEL

I have a problem using openia in the Laravel Framework 10.41.0 environment.

Whenever I send openIA requests to access the API the following errors occur. Failed to load resource: the server responded with a status of 500 (Internal Server Error) and when clicking on this error in the browser console it takes me to the /chat route that I created for the POST method. And there the following error is sent to me: The GET method is not supported for route chat. Supported methods: POST.

I have already applied the following commands to the solutions, but without success:

php artisan config:clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
composer dump-autoload

Scenario:

I use ajax as a script at the end of my layout.blade.php screen which has @yield('content') in the middle of it and my index.blade.php screen has the chat where I send requests.

AJAX:

$.ajaxSetup({
     headers: {
       'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
     }
   })
   $('#btn-submit').on('click', function(){
     $message = $('#message').val();
     $('#historic').append(`<div class="mb-2">
     <div class="box-my-message">
       <p class="my-message"> `+$message+` </p>
     </div>
     </div>`);
    

     $.ajax({
       type: 'post',
       url: '{{url('chat')}}',
       date: {
         'input': $message
       },
       success: function(data){
         $('#historic').append(`<div class="d-flex mb-2">

           <div class="box-response-message">
               <p class="my-message"> `+data+` </p>
           </div>

         </div>
         `)
         $message = $('#message').val('');
       }
      
     });
   });

My routes:

Route::post('/chat', [ChatController::class, 'chat']);

My Controller:

<?php

class ChatController extends Controller
{
     public function chat(Request $request)
     {
         if ($request->input == null) {
             return "Empty.";
         }

         $message = $request->input;

         $result = OpenAI::chat()->create([
             'model' => 'gpt-3.5-turbo',
             'messages' => [
                 ['role' => 'user', 'content' => 'Hello!'],
             ],
         ]);

         echo $result->choices[0]->message->content; // Hello! How can I watch you today?

        
        $result = $client->completions()->create([
             "model" => "gpt-3.5-turbo",
             "temperature" => 0.5,
             'max_tokens' => 4096,
             'prompt' => sprintf('Write article about: %s', $message),
         ]);
  
         $content = trim($result['choices'][0]['text']);
    
        
         return $content;
     }
}

I have also added the variables with the key in the .env. I simply cannot understand where this problem comes from and what the possible solutions are.

0 likes
7 replies
teeusantos's avatar

Controller updated but continue bug:

class ChatController extends Controller
{
    public function chat(Request $request)
    {
        if ($request->input == null) {
            return "Vazio.";
        }

        $message = $request->input;        

        $result = OpenAI::chat()->create([
            'model' => 'gpt-3.5-turbo',
            'messages' => [
                ['role' => 'user', 'content' => $message],
            ],
        ]);

        return $result->choices[0]->message->content;
    }
}
JussiMannisto's avatar

data is misspelled as date.

You really shouldn't be echoing stuff from the controller, it can cause all sorts of issues. Nothing after return "FOI" is ever executed because you're returning from the method.

Check storage/logs/laravel.log to see what is causing the 500 error.

1 like
teeusantos's avatar

@JussiMannisto Regarding the "Ok" return, I was just trying to find the part of the code that wasn't working, so I put it at different times, and on the first line after:

$result = OpenAI::chat()->create([
       'model' => 'gpt-3.5-turbo',
       'messages' => [
           ['role' => 'user', 'content' => 'Hello!'],
       ],
   ]);

This already caused exactly the same error. I made some changes to avoid confusion.

JussiMannisto's avatar

@teeusantos Did you check the log file like I suggested?

Please use three backticks in your posts to make your code readable:

```

$codeGoes = 'here';

```

Which will render like this:

$codeGoes = 'here';
1 like
teeusantos's avatar

@JussiMannisto Thanks, I didn't know how to format the code :D

Here's what I found in the log files:

[2024-01-21 00:32:29] local.ERROR: syntax error, unexpected variable "$message", expecting ";" {"exception":"[object] (ParseError(code: 0): syntax error, unexpected variable \"$message\", expecting at \app\Http\Controllers\ChatController.php:18)
[stacktrace]

My Controller updated:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use openAI\Laravel\Facades\OpenAI;

class ChatController extends Controller
{
     public function chat(Request $request)
     {
         if ($request->input == null) {
             return "Vazio.";
         }

         $result = OpenAI::chat()->create([
             'model' => 'gpt-3.5-turbo',
             'messages' => [
                 ['role' => 'user', 'content' => 'Hello!'],
             ],
         ]);

         return $result->choices[0]->message->content;

    }
}

Please or to participate in this conversation.