Muhammad
Muhammad

Reputation: 631

Restful API with Laravel not working properly

I've created a Restful API using Laravel 5.4. I am using postman to test this API. I am sending postcode and building_number in the body of the request. when I click on the Send button, I am getting Status code 200 OK in the postman console but only getting postcode value in the response and not the building_number. Also, not getting the response message i.e.

  'msg' => 'found postcode',

Below is the screen shot from postman

Postman response

My route file is as

web.php

Route::group(['prefix' => 'api/v1'], function(){

    Route::post('paf', ['uses' => 'PafController@getAddress']);

});

and the controller is

PafController

public function getAddress(Request $request)
{
    $postcode = $request->input('postcode');
    $building_number = $request->input('building_number');

    $result = [
        'post_code' => $postcode,
        'building_number' => $building_number
    ];

    $response = [
        'msg' => 'found postcode',
        'address' => $result
    ];

    return response()->json($response, 200);
   }

please can someone advise why I am not getting back the full response?

Upvotes: 0

Views: 644

Answers (1)

Christian Ahidjo
Christian Ahidjo

Reputation: 41

Define your route inside routes/api.php instead since it's an api you're building. This will automatically make laravel-rest.dev/api/paf available to you. All routes inside routes/api.php are prefixed with /api

Route::post('paf', ['uses' => 'PafController@getAddress']);

In postman select form-data option, right below body, instead of raw

Upvotes: 1

Related Questions