【问题标题】:Laravel localhost api server error (truncated...)Laravel localhost api 服务器错误(截断...)
【发布时间】:2020-06-11 09:33:52
【问题描述】:

我是这个 api 的新手,我明白了基本的想法,所以我跟着这个教程:https://appdividend.com/2018/04/17/laravel-guzzle-http-client-example/

但是每次我尝试滚动时,它都会给我这个错误

api 调用者控制器:

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 调用者路由:

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

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

api 提供者控制器:

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 提供者路由:

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

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

api调用者服务于8000端口,提供者服务于8001端口

提前致谢

【问题讨论】:

  • 你运行迁移了吗?表guzzle_posts 存在吗?
  • 尝试使用 Postman 对 localhost:8001/api/store 进行 POST 调用。你那里有一些错误。可能是迁移或数据库连接。

标签: php laravel api


【解决方案1】:

我认为您忘记了运行迁移并且不存在 guzzle_posts 表。看看吧

【讨论】:

    猜你喜欢
    • 2017-10-16
    • 1970-01-01
    • 2016-07-29
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多