Laravel-Guzzle调用POST返回GET

时间:2019-06-17 19:18:33

标签: php laravel guzzle

我正在尝试在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');
        });
    }
}

post端点应该允许我添加一个项目;但是,它并没有添加,只是返回了当前保留的项目列表。

0 个答案:

没有答案