Pranav Mandlik
Pranav Mandlik

Reputation: 644

method not allowed exception in postman

I am writing API login register system for my app. I have created a controller for login and register, so I can create my user from postmen and log in from there but when I try to call my get details method it's returning

as a method not allowed an exception

here is my Controller

<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Auth;
use Validator;
class PassportController extends Controller
{
//
  public $successStatus = 200;


  public function register(Request $request){
    $validator = Validator::make($request->all(), [
        'name' => 'required',
        'email' => 'required|email',
        'password' => 'required',
        'c_password' => 'required|same:password',
    ]);
    if ($validator->fails()) {
                return response()->json(['error'=>$validator->errors()], 401);            
            }
    $input = $request->all();
    $input['password'] = bcrypt($input['password']);
    $user = User::create($input);

    $success['token'] =  $user->createToken('MyApp')->accessToken;
    $success['name'] =  $user->name;
    return response()->json(['success'=>$success], $this->successStatus);
}

public function login(){
    if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){

        $user = Auth::user();
        $success['token'] =  $user->createToken('MyApp')->accessToken;
        return response()->json(['success' => $success], $this->successStatus);
    }
    else{
        return response()->json(['error'=>'Unauthorised'], 401);
    }
}


public function getDetails()
{
    $user = Auth::user();
    return response()->json(['success' => $user], $this->successStatus);
}
}

api.php

 <?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('register', 'API\PassportController@register')->name('register');
Route::post('login', 'API\PassportController@login')->name('login');



Route::group(['middleware' => 'auth:api'], function(){
 Route::post('details', 'API\PassportController@getDetails');
});

here is my postmen screenshot

enter image description here

you can share me your thoughts or any ideas

Upvotes: 0

Views: 4462

Answers (3)

Affan
Affan

Reputation: 1140

Please add your function url in app->http->Middleware->VerifyCsrfToken in $except .

laravel need csrf . if you pass data from outside it will not pass csrf so at that time you have to define for this function no csrf required .

Upvotes: 2

Indra
Indra

Reputation: 702

Are you using passport with laravel? If yes there might be multiple issues causing this.

I would like to see what you get when you click on application frames, but here are some of the more common issues i found myself dealing with

General issues I know of (regardless of technology)

  1. method is not public

  2. Authorization and Accept should be in table Headers in postman enter image description here

when using the Authorization it need to be like this: Headers tab

key: Authorization Value: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiI

where this gibberish is your access token without any commas

  1. check you server and laravel logs. Sometimes postman messes up and send get instead of post requests and the other way around

  2. If you get Unauthenticated error

4.1 follow these steps here: https://laravel.com/docs/5.6/passport

4.2 copy the client secret from the db/command line

4.3 use this endpoint in the picture enter image description here {{url_key}} id the address to my site ex: demo.dev or htttp://localhost. I'm not sure about using locahost (I only developed laravel on vagrant so i don't know what issues may arise from that) Headers should be empty in this case

4.4 use the token to authenticate

  1. Make sure VerifyCsrfToken class looks like this
 class VerifyCsrfToken extends BaseVerifier
    {
        /**
         * The URIs that should be excluded from CSRF verification.
         *
         * @var array
         */
        protected $except = [
            'api/*'
        ];
    }

Upvotes: 3

Mehravish Temkar
Mehravish Temkar

Reputation: 4365

You method is getDetails() while your route is post

Route::post('details', 'API\PassportController@getDetails');

Try changing getDetails() to postDetails() and change route as

Route::post('details', 'API\PassportController@postDetails');

Upvotes: 2

Related Questions