0

I'm testing an API call that I have working in Postman and other interfaces built over the API, but I'm trying to actually get my return values within my laravel site.

Should be simple enough, I made a very bare service file that uses guzzle to get to the API url, and I have a function in there for logging in using a test email, password and client ID (all of which work in the api interface such as Postman)

I'm calling the class and function inside my authController and then returning the function call in the view, then dumping that inside the view.

However, currently I'm getting null on my page dump.

Is there something I'm missing here, possibly in my service file. I don't think there should be anything passed here, but maybe I'm overlooking something more obvious.

Authorize.php

<?php
namespace App\Services\restAPI;

use Illuminate\Support\Facades\Auth;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;


use Session;
use Exception;

class AuthService
{

    protected $baseurl;
    protected $guzzleClient;

    public function  __construct() {
        $this->baseurl = config('http://ip.address');
        $this->baseurl = rtrim($this->baseurl, '/') . '/';  // ensure trailing slash

        $this->guzzleClient = new \GuzzleHttp\Client(
            ['verify'   => config('app.ca_pem_file'),
             'headers'  => [
                    'Event-ID' => ACCESS_EVENT_ID
                ],
             'base_uri' => $this->baseurl
            ]
        );
    }

    public function loginGetToken(){
        $password = "password";
        $email = "[email protected]";
        $client_id = "0";

        $payload = (object)[
            'password'=>(string)$password,
            'email'=>(string)$email
        ];

        $retval = $this->post('login/?client_id='.$client_id,$payload);

        return $retval;
    }

    private function post($endpoint,$payload){

        $result = $this->guzzleClient->request('POST', $endpoint, ['json'=>$payload]);
        $body = $result->getBody();
        return  json_decode($body);
    }
}

AuthController.php

use Redirect;
use Illuminate\Http\Request;
use App\Services\restAPI\AuthService;

class AuthController extends Controller
{
    public function login(Request $request)
    {
        $authService = new AuthService();
        $login = $authService->loginGetToken();

         return redirect(route('auth.login'))
            ->with('login', $login)
    }

login.blade.php

<?php

    dd($login);

?>

1 Answer 1

1

Since you are redirecting, the data is not directly available in the view as a variable. It should be in the session though, so you can try this in your view:

dd(session('login'));

https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data

Sign up to request clarification or add additional context in comments.

5 Comments

Hmm, that does make sense but still seems to show null
Is there a better way to try and return that and dump, just to test if it's returning what I expect in the call? This is a POST function but I need to see the body of it's response
You could just do the dump in your controller before hitting the view.
so I think that got me somewhere but In my page error now it just shows my URL with /login/?client_id=0 but I don't see that the payload was passed?
Not sure. You could dd($result) in your Authorize.php post() method to see what you are getting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.