php - Laravel - Guzzle 调用 POST 返回 GET
问题描述
我正在尝试在 Guzzle 中发出 POST 请求,但我正在调用的 API 正在返回响应,就好像我发出了 GET 请求一样。
控制器
class Controller extends BaseController
{
const URL = 'http://apidomain.com';
public function __construct()
{
$this->client = new \GuzzleHttp\Client();
}
public function createItem()
{
$body = [
'itemId' => 123,
];
$this->callPostRequest('api/items', $body);
}
private function callPostRequest($endpoint, $body)
{
$response = $this->client->post(self::URL.$endpoint, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => "Bearer {$this->getAccessToken()}",
],
'form_params' => $body
]);
dd(\GuzzleHttp\json_decode((string) $response->getBody()));
}
}
提供者
class ApiServiceProvider extends BaseServiceProvider
{
public function boot()
{
$this->loadViewsFrom(__DIR__.'/../Views', 'api');
parent::boot();
}
public function routes()
{
$group = [
'prefix' => '/api',
'namespace' => 'Api\Controllers',
];
Route::group($group, function () {
Route::get('/create-item', 'Controller@createItem')
->name('api::create-item');
});
}
}
后端点应该允许我添加一个项目;但是它没有添加,只是返回当前持久的项目列表。
解决方案
推荐阅读
- python - 如何使文本出现在圆圈中
- javascript - 将 Chrome 扩展程序移植到 Firefox 扩展程序时出现问题
- sql - SQL 问题:错误代码:1242。子查询返回多于 1 行
- php - 我的 $.ajax POST 方法在我的本地主机上工作,但当我将它上传到主机上时不起作用
- .net - AWS DotNet SDK 错误:无法从 EC2 实例元数据服务获取 IAM 安全凭证
- c - 接受四个用户输入并与 C 中的 strcmp 进行比较
- javascript - 是否有一个函数可以根据当前语言环境为我提供日期格式
- c - 操作虚拟地址时出现总线错误
- python-3.x - 在预处理图像后检测白色文本以使用 OCR + tesseract 4 (python) 提取它
- botframework - QnARequestContext.previousUserQuery 在 QnA Maker 选项中用于生成答案 API(QnA Maker)的目的是什么?