首页 > 解决方案 > 使用 id 参数发出 HTTP 请求

问题描述

我有 2 个项目在两个不同的端口上运行

在另一个项目中,我写了这条路线

项目A

Route::get("/getBook/{id}", function ($id) {
    return response()->json(BlogArticle::find($id));
});

该路线将用作其他项目的 api,以获取所需的书籍。在另一个项目中,我想通过在 url 我的方法中发送一个 id 作为参数 {id} 来检索这本书

Route::get("/api/testing", function () {
    $response = Http::get('http://127.0.0.1:900/api/getBook', [
        'id' => 1
    ]);
    
    return $response;
});

但是没有返回任何内容我收到 404 not found 错误?

我错过了什么?

标签: laravelhttp

解决方案


由于路由定义是,这意味着将处理对该 URL的Route::get('/getBook/{id}')任何请求。GET{id}部分是 URL 中的占位符,它不表示查询字符串部分。因此,您必须在 URL 本身中包含 ID:

$response = Http::get('http://127.0.0.1:900/api/getBook/1')

推荐阅读