laravel - 使用 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 错误?
我错过了什么?
解决方案
由于路由定义是,这意味着将处理对该 URL的Route::get('/getBook/{id}')
任何请求。GET
该{id}
部分是 URL 中的占位符,它不表示查询字符串部分。因此,您必须在 URL 本身中包含 ID:
$response = Http::get('http://127.0.0.1:900/api/getBook/1')
推荐阅读
- javascript - 如何将道具作为对象传递
- html - 如果选中单选按钮,则仅使用 CSS 和 HTML 为 texarea 设置“必需”属性
- python - Python 3 字典返回不同的值
- python - 无法腌制日期时间子类
- apache-spark - Get certain percentage data on each values of a column using spark java
- python - 从 django 应用程序运行 python 脚本,在 Raspberry Pi 上显示窗口
- c++ - 赋值给 C++ 中不存在的对象
- node.js - 我对 Node JS 服务器的第一次浏览器 Ajax 调用(不起作用)
- visual-studio - 如何使用 Visual Studio 2019 在 Docker 容器中运行 ASP.NET Core 3.1 项目?
- php - sql没有填充php