简体   繁体   中英

Laravel localhost api server error (truncated...)

i'm new to this api thing, i get the basic idea, so i followed this tutorial: https://appdividend.com/2018/04/17/laravel-guzzle-http-client-example/

but everytime i tried rolling, it gives me this error

错误信息

api caller controller:

class DataController extends Controller
{
    public function postRequest()
    {
        $client = new \GuzzleHttp\Client();
        $response = $client->request('POST', 'http://localhost:8001/api/store', [
            'form_params' => [
                'name' => 'krunal',
            ]
        ]);
        $response = $response->getBody()->getContents();
        echo '<pre>';
        print_r($response);
    }

    public function getRequest()
    {
        $client = new \GuzzleHttp\Client();
        $request = $client->get('http://localhost:8001/api/index');
        $response = $request->getBody()->getContents();
        echo '<pre>';
        print_r($response);
        exit;
    }
}

api caller route:

Route::get('/', function () {
    return view('welcome');
});

Route::get('post','DataController@postRequest');
Route::get('get','DataController@getRequest');

api provider controller:

class GuzzlePostController extends Controller
{
    public function store(Request $request)
    {
        $data = new GuzzlePost();
        $data->name=$request->get('name');
        $data->save();
        return response()->json('Successfully added');

    }

    public function index()
    {
        $data = GuzzlePost::all();
        return response()->json($data);
    }
}

api provider route:

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

Route::post('store', 'GuzzlePostController@store');
Route::get('index', 'GuzzlePostController@index');

the api caller served on port 8000, the provider served on port 8001

thanks in advance

I think that you forgot run migrations and there no guzzle_posts table exists. Check it out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM