In my routes/api.php
I have a POST method route as such:
Route::post('hello', 'MyController@greet');
In the app/Http/Controllers/MyController.php
the greet()
method is defined as such:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class MyController extends Controller
{
public function greet(Request $request)
{
return response()->json(['not found'], 404);
}
}
Now, depending on the URL protocol that was calling from, the response is returning totally different messages:
Calling from POST https://localhost/hello will return 404 Not Found response:
[
"not found"
]
as expected.
Wheres, when calling from POST http://localhost/hello (without https) it will return 405 Method Not Allowed response instead:
{
"message": "The GET method is not supported for this route. Supported methods: POST."
}
This is a problem because I'm setting up my server behind AWS ELB which accepts HTTPS traffic only but will route back to the Auto Scaling instances via HTTP within the local VPC network.
So my question is what is causing this and how can I prevent Laravel from returning a different response based on URL protocol?
EDIT: Note that this only happens when I specify the status code in the response()->json($data, [status code])
. Otherwise, the response works normally as expected.