Home Restful API with Laravel not working properly
Reply: 1

Restful API with Laravel not working properly

Muhammad
1#
Muhammad Published in 2018-01-13 00:55:14Z

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

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?

Christian Ahidjo
2#
Christian Ahidjo Reply to 2018-01-13 01:27:26Z

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.36705 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO