jeudi 7 septembre 2017

Error (405) Method Not Allowed when using POST

I am using Laravel 5.4 with JWTAuth & Dingo and for some reason I am now no longer able to make POST requests with Postman. This was working when I first set it up, but not I get the response 405 Method Not Allowed This seems to have been raised a few times on here, but I can't seem to find a solution. I've cleared the route cache, and when i do api:routes the correct routes are in there.

Below is the routes file, and the controllers it should be sending too. I am only having the issue with the LeadController routes.

api.php

use Dingo\Api\Routing\Router;

/** @var Router $api */
$api = app(Router::class);

$api->version('v1', function (Router $api) {
    $api->group(['prefix' => 'auth'], function(Router $api) {
        //$api->post('signup', 'App\\Api\\V1\\Controllers\\SignUpController@signUp');
        $api->post('login', 'App\\Api\\V1\\Controllers\\LoginController@login');

        $api->post('recovery', 'App\\Api\\V1\\Controllers\\ForgotPasswordController@sendResetEmail');
        $api->post('reset', 'App\\Api\\V1\\Controllers\\ResetPasswordController@resetPassword');
    });

    $api->group(['middleware' => 'jwt.auth'], function(Router $api) {
        $api->get('protected', function() {
            return response()->json([
                'message' => 'Access to protected resources granted! You are seeing this text as you provided the token correctly.'
            ]);
        });

        $api->get('refresh', [
            'middleware' => 'jwt.refresh',
            function() {
                return response()->json([
                    'message' => 'By accessing this endpoint, you can refresh your access token at each request. Check out this response headers!'
                ]);
            }
        ]);

        $api->post('lead/store', 'App\\Api\\V1\\Controllers\\LeadController@store');
        $api->get('lead', 'App\\Api\\V1\\Controllers\\LeadController@index');

    });
});

LeadController.php

namespace App\Api\V1\Controllers;

use Symfony\Component\HttpKernel\Exception\HttpException;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use Dingo\Api\Routing\Helpers;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Lead;
use App\User;

class LeadController extends Controller
{
    use Helpers;

    public function index(Lead $leads)
    {
        $leads = $leads->all();

        $count = 0;
        foreach($leads as $key => $lead){
            $user = User::where('id', $lead->user_id)->first();
            $leads[$count]['name'] = $user->name;

            array_pull($leads[$count], 'user_id');
            $count++;
        }

        return $leads;
    }

    public function store(Request $request)
    {

        $today = new Carbon();

        $this->validate(request(), [
            'owner' => 'required',
            'bname' => 'required|min:3|max:255',
            'tname' => 'max:255',
            'created' => 'required|date|before_or_equal:today',
            'update' => 'date'
        ]);

        if(!$user = User::where('zoho_id', $request->get('owner'))->first())
            return $this->response->error('invalid_owner', 500);

        $lead = new Lead;
        $lead->user_id = $user->id;
        $lead->bname = $request->get('bname');
        $lead->tname = $request->get('tname');
        $lead->created_at = $request->get('created');
        $lead->updated_at = $request->get('updated');

        if($lead->save())
            return $this->response->created();
        else
           return $this->response->error('could_not_create_lead', 500);
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire