Fredericka Hartman
Fredericka Hartman

Reputation: 853

Laravel HTTP Client post request not working

Try to get Access Token From Instagram

       $params = array(
            'app_id' => $this->clientId,
            'app_secret' => $this->clientSecret,
            'grant_type' => 'authorization_code',
            'redirect_uri' => $this->redirectUrl,
            'code' => $this->request->code
        );
        $ch = curl_init();
        $endpoint = $this->getBaseUrl() . 'oauth/access_token';
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $endpoint);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 1);


        $response = curl_exec($ch);

        curl_close($ch);
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header = substr($response, 0, $header_size);
        $body = substr($response, $header_size);
        dd($response,$header_size,$header,$body);
        $response = json_decode($response, true);
        return $response;

this code with CURL working fine.

Request Data Laravel Documentation.

$response = Http::post($this->getBaseUrl() . 'oauth/access_token', [
            'app_id' => $this->clientId,
            'app_secret' => $this->clientSecret,
            'grant_type' => 'authorization_code',
            'redirect_uri' => $this->redirectUrl,
            'code' => $this->request->code
        ]
    );
   return $response->body();

but not working with Laravel Http Client.it return Client Id is missing but I'm sending as parameter.

Upvotes: 2

Views: 5036

Answers (1)

bhucho
bhucho

Reputation: 3420

I guess your content type is application/x-www-form-urlencoded, try to use asForm(),

$response = Http::asForm()->post($this->getBaseUrl() . 'oauth/access_token', [
            'app_id' => $this->clientId,
            'app_secret' => $this->clientSecret,
            'grant_type' => 'authorization_code',
            'redirect_uri' => $this->redirectUrl,
            'code' => $this->request->code
        ]
    );
return $response->json();

Upvotes: 6

Related Questions