douquanqiao6788 2019-07-08 18:52 采纳率: 100%
浏览 233

Laravel 404 JSON响应在HTTP和HTTPS中返回不同的消息

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.

  • 写回答

2条回答 默认 最新

  • douli4852 2019-07-08 19:08
    关注

    None of those has to work, because you have defined your route as a post. Your requests are 'get' requests. It seems there is middleware in a higher priority and it's being called when it's an HTTP request to control illegal requests.

    Try returning different things like dd("Hello World"); if it's loading the page there has to be a bug or something.

    Temporarily add a middleware to your route to redirect requests to one of HTTP or HTTPS protocols

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿