PoweredLocal / vrata

API gateway implemented in PHP and Lumen
MIT License
471 stars 116 forks source link

Cant't get post data when request a service by `POST` method #3

Open lijiebin opened 7 years ago

lijiebin commented 7 years ago

Because the data of setBody not in $_POST but in php://input

 private function simpleRequest(Request $request, RestClient $client)
    {
        if ($request->getRoute()->isAggregate()) throw new NotImplementedException('Aggregate ' . strtoupper($request->method()) . 's are not implemented yet');

        $client->setBody($request->getContent());

        if (count($request->allFiles()) !== 0) {
            $client->setFiles($request->allFiles());
        }

        $parametersJar = array_merge($request->getRouteParams(), ['query_string' => $request->getQueryString()]);
        $response = $client->syncRequest($this->actions->first()->first(), $parametersJar);

        return $this->presenter->format((string)$response->getBody(), $response->getStatusCode());
    }
sidigi commented 4 years ago

Add to RestClient


    /**
     * @param string $form params
     *
     * @return $this
     */
    public function setFormParams($params)
    {
        unset($this->guzzleParams['headers']['Accept'], $this->guzzleParams['headers']['Content-Type']);

        $this->guzzleParams['form_params'] = $params;

        return $this;
    }

Add to GatewayController

    private function simpleRequest(Request $request, RestClient $client)
    {
        //...
        if ($request->isMethod('post')) {
            $client->setFormParams($request->post());
        } else {
            $client->setBody($request->getContent());
        }
        //...
    }

Not the best solution