I have tried to send some values using post method in REST API Tool. I have used Laravel version 5.4. I have tried the following code
Route::post('ws-register',array('uses' => 'AppController@doRegister'));
public function doRegister() {
$rules = array(
'email' => 'required|email|unique:users',
'password' => 'required|alpha_num|min:6|max:15'
);
$messages = array('alpha_spaces' => 'Name must be alphanumeric');
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails()) {
$error = $validator->errors()->all(':message');
$response['message'] = $error[0];
$response['code'] = false;
} else {
$user = new Users;
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
$response['user_id'] = $user->id;
$response['message'] = "Success";
$response['code'] = true;
}
}
return Response::json($response);
}
While calling through REST API POST Method, I am getting "500: Internal Server Error" as response. Can anyone help me out to find what I have done wrong?
Assuming you are having TokenMismatchException in VerifyCsrfToken.php
error, as was written in question comments.
As stated in app/Http/Kernel.php
, VerifyCsrfToken middleware is applied to all routes in routes/web.php
. But it is not applied to routes/api.php
If you are making API request, may be it is better to place your route in routes/api.php
file. Then, when making API call, make sure to add /api
prefix to your request, like this:
POST http://example.app/api/ws-register
But if you still want to to keep your route in routes/web.php
, think of adding CSRF-protection. More info at https://laravel.com/docs/5.4/csrf