php - 拉维尔 5.2 : POST request is always returning "405 Method Not Allowed"

标签 php api laravel post

所以我正在使用 Laravel 5.2 开发一个 API,我面临一个重要的问题。

我有一个 UserController 来管理我的应用程序的用户。

这是我的 routes.php 文件:

Route::group(array('prefix' => 'api/v1'), function() {    
   Route::post('user', 'UserController@store');
});

我的 UserController 是这样定义的:

class UserController extends Controller {

   public function index() {
       return 'Hello, API';
   }

   public function create(){
   }

   public function store(Request $request) {
       $user = new User;
       $user->email = $request->email;
       $user->password = $request->password;
       $user->fbId = $request->fbId;
       $user->ggId = $request->ggId;
       $user->firstName = $request->firstName;
       $user->lastName = $request->lastName;
       $user->imageUrl = $request->imageUrl;
       $user->country = $request->country;
       $user->mobile = $request->mobile;
       $user->gender = $request->gender;
       $user->client = $request->client;

       $user->save();

       return Response::json(array(
           'error' => false,
           'userId' => $user->id),
           200
       );
   }

   public function update(Request $request, $id) {
   }
}

这是 php artisan route:list 的输出

+--------+--------+-------------+------+-------------------------------------------+------------+
| Domain | Method | URI         | Name | Action                                    | Middleware |
+--------+--------+-------------+------+-------------------------------------------+------------+
|        | POST   | api/v1/user |      | App\Http\Controllers\UserController@store | web        |
+--------+--------+-------------+------+-------------------------------------------+------------+

我正在使用 Postman 来测试我的 POST 请求。 每次我向/api/v1/user 发出 POST 请求时,我都会收到“405 Method Not Allowed”错误。

我错过了什么吗?

我应该做些什么来解决这个问题?

最佳答案

我和你有同样的问题,我已经在我的 POST 路由中设置了例如“/api/v1/user”,

当我尝试使用 POSTMAN(测试 API 的应用程序)连接时,它返回 405-Method Not Allowed,

然后我意识到我发送的 url 使用的是“http”,然后我将其更改为“https”(后面有“s”)
然后我的 API 正常工作!

通常如果我们与不同的服务器进行交互,我们必须使用'https'
但是如果您的应用程序在同一台服务器上,
可以使用'http '

我的情况的真正原因是与任何不同服务器的任何交互都必须使用“https”(这是我服务器上的设置)

关于php - 拉维尔 5.2 : POST request is always returning "405 Method Not Allowed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37836223/

相关文章:

node.js - git 推送的更改未反射(reflect)在 heroku 服务器上

php - Laravel Value() 函数未返回正确的值

php - laravel5 迁移复制现有表

javascript - 单击日期重定向到页面

php - 使用 htaccess 进行自动版本控制 : htaccess regex Rewrite rule not picking up pattern

php - 如何显示来自 mysql blob 的图像

javascript - 获取 javascript 对象中共享同级同级的所有值

php - 多次准备一个语句不好吗?

wordpress - Woocommerce REST API 扩展订单响应

php - Laravel - Sentinel 用户与 eloquent 用户不同