Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

php - Laravel NotFoundHttpException on API

I have API URL like:

http://example.com/api/driverAcceptOrder?id=bee74e39-ff38-46a6-9e5d-6db799d2be8c&driverId=3453a3a9-7f58-434a-8dab-95c3469e6238

method is POST and it takes 2 parameter id and driverId

When I try to run this URL in postman I get:

Symfony\Component\HttpKernel\Exception\NotFoundHttpException

Route

Route::post('driverAcceptOrder/{id}/{driverId}', 'ApiDriversController@driverAcceptOrder');

Controller

public function driverAcceptOrder(Request $request, $id, $driverId)
{
    $order = Order::findOrFail($id);
    $defs = OrderDefaultProgress::where('name', 'Driver OTW ke pelanggan')->first();
    $driver = Driver::where('id', $driverId)->with('user')->first();
    $order->update(['driver_id' => $driverId]);
    return response()->json([
        'data' => $driver,
        'message' => 'Pengemudi Dalam perjalanan menuju pelanggan.'
    ], 200);
}

Note

  1. Route is not restricted by Auth middleware (its public)
  2. I've added exception to my VerifyCsrfToken file as protected $except = ['/api/*'];

Any idea?

question from:https://stackoverflow.com/questions/65839805/laravel-notfoundhttpexception-on-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

your url is wrong

example.com/api/driverAcceptOrder?id=bee74e39-ff38-46a6-9e5d-6db799d2be8c&driverId=3453a3a9-7f58-434a-8dab-95c3469e6238

here after ? all is query paramter which is used in GET method to send data

Route::get('driverAcceptOrder',"..");

which is not found in your case that's why your getting

NotFoundHttpException

for your case url should be

example.com/api/driverAcceptOrder/bee74e39/3453a3a9-7f58-434a-8dab-95c3469e6238

this will be handel by

Route::post('driverAcceptOrder/{id}/{driverId}', 'ApiDriversController@driverAcceptOrder');

you can learn more about

GET and POST here https://www.w3schools.com/tags/ref_httpmethods.asp


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...