Laravel 5.2:POST请求始终返回"405 Method Not Allowed". [英] Laravel 5.2: POST request is always returning "405 Method Not Allowed"
问题描述
因此,我正在使用Laravel 5.2开发API,并且遇到了一个重要问题.
So I am developing an API with Laravel 5.2 and I'm facing an important issue.
我有一个UserController,它将管理我的应用程序的用户.
I have a UserController that will manage the users of my app.
这是我的route.php文件:
This is my routes.php file:
Route::group(array('prefix' => 'api/v1'), function() {
Route::post('user', 'UserController@store');
});
我的UserController定义如下:
And I have my UserController defined like that:
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方法不允许"错误.
I'm using Postman to test my POST requests. Every time I make a POST request to /api/v1/user, I get a "405 Method Not Allowed" error.
我错过了什么吗?
我应该做些什么来解决此问题?
Is there anything I should do to fix this issue?
推荐答案
我在POST路由中已经设置了相同的问题,例如"/api/v1/user",
并且当我尝试使用POSTMAN(应用程序测试API)进行连接时,它返回405-Method Not Allowed,
I have same problem with you which are I already set in my POST route for example "/api/v1/user",
and when I try to connect using POSTMAN (application to test API) , it return 405-Method Not Allowed,
然后我意识到发送给我的网址是使用' http ',然后将其更改为' https '(后面是's')
然后我的API可以正常工作!
and then i realize the url i was sent is using 'http' and after i change it to 'https' (with the 's' at the back)
then my API working like normal !
通常,如果我们与其他服务器交互,则必须使用' https '
,但是如果您的应用程序位于同一服务器上,
可以使用' http '
我这种情况的真正原因是与任何其他服务器的任何交互都必须使用" https "(这是我服务器上的设置)
normally if we interact with different server , we must use 'https'
but if your application at the same server ,
it's okay to use 'http'
The real reason for my case is any interaction with any different server must use 'https' (This is the setup on my server)
这篇关于Laravel 5.2:POST请求始终返回"405 Method Not Allowed".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!