Is There A Way To Include Data With Http/Guzzle Response In Laravel












0














So I am trying to return User data with login request. I am using guzzle and I do not know how to attach data to response.



Here is the login method



public function login(Request $request)
{
$http = new GuzzleHttpClient;

try {
$response = $http->post(config('services.passport.login_endpoint'), [
'form_params' => [
'grant_type' => 'password',
'client_id' => config('services.passport.client_id'),
'client_secret' => config('services.passport.client_secret'),
'username' => $request->username,
'password' => $request->password,
]
]);


return $response->getBody();
} catch (GuzzleHttpExceptionBadResponseException $e) {
if ($e->getCode() === 400) {
return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
} else if ($e->getCode() === 401) {
return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
}
return response()->json('Something went wrong on the server.', $e->getCode());
}
}


I would like to do something like this



$user = User::where('email', $request->username)->get();

$token = $response->getBody();

return response()->json($token, $user);


However when I try this I get error. Any help would be greatly appreciated. Currently I am having to make separate request after access token to return the user data thus I would like for it to happen at the same time....










share|improve this question



























    0














    So I am trying to return User data with login request. I am using guzzle and I do not know how to attach data to response.



    Here is the login method



    public function login(Request $request)
    {
    $http = new GuzzleHttpClient;

    try {
    $response = $http->post(config('services.passport.login_endpoint'), [
    'form_params' => [
    'grant_type' => 'password',
    'client_id' => config('services.passport.client_id'),
    'client_secret' => config('services.passport.client_secret'),
    'username' => $request->username,
    'password' => $request->password,
    ]
    ]);


    return $response->getBody();
    } catch (GuzzleHttpExceptionBadResponseException $e) {
    if ($e->getCode() === 400) {
    return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
    } else if ($e->getCode() === 401) {
    return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
    }
    return response()->json('Something went wrong on the server.', $e->getCode());
    }
    }


    I would like to do something like this



    $user = User::where('email', $request->username)->get();

    $token = $response->getBody();

    return response()->json($token, $user);


    However when I try this I get error. Any help would be greatly appreciated. Currently I am having to make separate request after access token to return the user data thus I would like for it to happen at the same time....










    share|improve this question

























      0












      0








      0







      So I am trying to return User data with login request. I am using guzzle and I do not know how to attach data to response.



      Here is the login method



      public function login(Request $request)
      {
      $http = new GuzzleHttpClient;

      try {
      $response = $http->post(config('services.passport.login_endpoint'), [
      'form_params' => [
      'grant_type' => 'password',
      'client_id' => config('services.passport.client_id'),
      'client_secret' => config('services.passport.client_secret'),
      'username' => $request->username,
      'password' => $request->password,
      ]
      ]);


      return $response->getBody();
      } catch (GuzzleHttpExceptionBadResponseException $e) {
      if ($e->getCode() === 400) {
      return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
      } else if ($e->getCode() === 401) {
      return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
      }
      return response()->json('Something went wrong on the server.', $e->getCode());
      }
      }


      I would like to do something like this



      $user = User::where('email', $request->username)->get();

      $token = $response->getBody();

      return response()->json($token, $user);


      However when I try this I get error. Any help would be greatly appreciated. Currently I am having to make separate request after access token to return the user data thus I would like for it to happen at the same time....










      share|improve this question













      So I am trying to return User data with login request. I am using guzzle and I do not know how to attach data to response.



      Here is the login method



      public function login(Request $request)
      {
      $http = new GuzzleHttpClient;

      try {
      $response = $http->post(config('services.passport.login_endpoint'), [
      'form_params' => [
      'grant_type' => 'password',
      'client_id' => config('services.passport.client_id'),
      'client_secret' => config('services.passport.client_secret'),
      'username' => $request->username,
      'password' => $request->password,
      ]
      ]);


      return $response->getBody();
      } catch (GuzzleHttpExceptionBadResponseException $e) {
      if ($e->getCode() === 400) {
      return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
      } else if ($e->getCode() === 401) {
      return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
      }
      return response()->json('Something went wrong on the server.', $e->getCode());
      }
      }


      I would like to do something like this



      $user = User::where('email', $request->username)->get();

      $token = $response->getBody();

      return response()->json($token, $user);


      However when I try this I get error. Any help would be greatly appreciated. Currently I am having to make separate request after access token to return the user data thus I would like for it to happen at the same time....







      laravel guzzle






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 19:37









      TJ WeemsTJ Weems

      14219




      14219
























          2 Answers
          2






          active

          oldest

          votes


















          0














          The first argument of json method can be an array:



          return response()->json(['token' => $token, 'user' => $user ]);





          share|improve this answer





















          • thank you for the help. For some reason $token is returning empty object. Any ideas?
            – TJ Weems
            Nov 21 '18 at 22:46












          • Can you post the code?
            – Arthur Samarcos
            Nov 21 '18 at 23:01










          • I was recommended this discusstion and it worked. I will post my answer.
            – TJ Weems
            Nov 21 '18 at 23:11



















          0














          I was recommended the following discussion and it solved my issue. here is the final code



          public function login(Request $request)
          {
          $http = new GuzzleHttpClient;

          try {
          $response = $http->post(config('services.passport.login_endpoint'), [
          'form_params' => [
          'grant_type' => 'password',
          'client_id' => config('services.passport.client_id'),
          'client_secret' => config('services.passport.client_secret'),
          'username' => $request->username,
          'password' => $request->password,
          ]
          ]);

          $token = $response->getBody();

          $data = json_decode($token, true);

          $user = User::where('email', $request->username)->with('role.rules')->get();

          $rules = $user->pluck('role')->collapse()->pluck('rules');

          $rules->put('access_token', $data['access_token']);

          return response()->json($rules);
          } catch (GuzzleHttpExceptionBadResponseException $e) {
          if ($e->getCode() === 400) {
          return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
          } else if ($e->getCode() === 401) {
          return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
          }
          return response()->json('Something went wrong on the server.', $e->getCode());
          }
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419399%2fis-there-a-way-to-include-data-with-http-guzzle-response-in-laravel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The first argument of json method can be an array:



            return response()->json(['token' => $token, 'user' => $user ]);





            share|improve this answer





















            • thank you for the help. For some reason $token is returning empty object. Any ideas?
              – TJ Weems
              Nov 21 '18 at 22:46












            • Can you post the code?
              – Arthur Samarcos
              Nov 21 '18 at 23:01










            • I was recommended this discusstion and it worked. I will post my answer.
              – TJ Weems
              Nov 21 '18 at 23:11
















            0














            The first argument of json method can be an array:



            return response()->json(['token' => $token, 'user' => $user ]);





            share|improve this answer





















            • thank you for the help. For some reason $token is returning empty object. Any ideas?
              – TJ Weems
              Nov 21 '18 at 22:46












            • Can you post the code?
              – Arthur Samarcos
              Nov 21 '18 at 23:01










            • I was recommended this discusstion and it worked. I will post my answer.
              – TJ Weems
              Nov 21 '18 at 23:11














            0












            0








            0






            The first argument of json method can be an array:



            return response()->json(['token' => $token, 'user' => $user ]);





            share|improve this answer












            The first argument of json method can be an array:



            return response()->json(['token' => $token, 'user' => $user ]);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 20:12









            Arthur SamarcosArthur Samarcos

            2,3751420




            2,3751420












            • thank you for the help. For some reason $token is returning empty object. Any ideas?
              – TJ Weems
              Nov 21 '18 at 22:46












            • Can you post the code?
              – Arthur Samarcos
              Nov 21 '18 at 23:01










            • I was recommended this discusstion and it worked. I will post my answer.
              – TJ Weems
              Nov 21 '18 at 23:11


















            • thank you for the help. For some reason $token is returning empty object. Any ideas?
              – TJ Weems
              Nov 21 '18 at 22:46












            • Can you post the code?
              – Arthur Samarcos
              Nov 21 '18 at 23:01










            • I was recommended this discusstion and it worked. I will post my answer.
              – TJ Weems
              Nov 21 '18 at 23:11
















            thank you for the help. For some reason $token is returning empty object. Any ideas?
            – TJ Weems
            Nov 21 '18 at 22:46






            thank you for the help. For some reason $token is returning empty object. Any ideas?
            – TJ Weems
            Nov 21 '18 at 22:46














            Can you post the code?
            – Arthur Samarcos
            Nov 21 '18 at 23:01




            Can you post the code?
            – Arthur Samarcos
            Nov 21 '18 at 23:01












            I was recommended this discusstion and it worked. I will post my answer.
            – TJ Weems
            Nov 21 '18 at 23:11




            I was recommended this discusstion and it worked. I will post my answer.
            – TJ Weems
            Nov 21 '18 at 23:11













            0














            I was recommended the following discussion and it solved my issue. here is the final code



            public function login(Request $request)
            {
            $http = new GuzzleHttpClient;

            try {
            $response = $http->post(config('services.passport.login_endpoint'), [
            'form_params' => [
            'grant_type' => 'password',
            'client_id' => config('services.passport.client_id'),
            'client_secret' => config('services.passport.client_secret'),
            'username' => $request->username,
            'password' => $request->password,
            ]
            ]);

            $token = $response->getBody();

            $data = json_decode($token, true);

            $user = User::where('email', $request->username)->with('role.rules')->get();

            $rules = $user->pluck('role')->collapse()->pluck('rules');

            $rules->put('access_token', $data['access_token']);

            return response()->json($rules);
            } catch (GuzzleHttpExceptionBadResponseException $e) {
            if ($e->getCode() === 400) {
            return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
            } else if ($e->getCode() === 401) {
            return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
            }
            return response()->json('Something went wrong on the server.', $e->getCode());
            }
            }





            share|improve this answer


























              0














              I was recommended the following discussion and it solved my issue. here is the final code



              public function login(Request $request)
              {
              $http = new GuzzleHttpClient;

              try {
              $response = $http->post(config('services.passport.login_endpoint'), [
              'form_params' => [
              'grant_type' => 'password',
              'client_id' => config('services.passport.client_id'),
              'client_secret' => config('services.passport.client_secret'),
              'username' => $request->username,
              'password' => $request->password,
              ]
              ]);

              $token = $response->getBody();

              $data = json_decode($token, true);

              $user = User::where('email', $request->username)->with('role.rules')->get();

              $rules = $user->pluck('role')->collapse()->pluck('rules');

              $rules->put('access_token', $data['access_token']);

              return response()->json($rules);
              } catch (GuzzleHttpExceptionBadResponseException $e) {
              if ($e->getCode() === 400) {
              return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
              } else if ($e->getCode() === 401) {
              return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
              }
              return response()->json('Something went wrong on the server.', $e->getCode());
              }
              }





              share|improve this answer
























                0












                0








                0






                I was recommended the following discussion and it solved my issue. here is the final code



                public function login(Request $request)
                {
                $http = new GuzzleHttpClient;

                try {
                $response = $http->post(config('services.passport.login_endpoint'), [
                'form_params' => [
                'grant_type' => 'password',
                'client_id' => config('services.passport.client_id'),
                'client_secret' => config('services.passport.client_secret'),
                'username' => $request->username,
                'password' => $request->password,
                ]
                ]);

                $token = $response->getBody();

                $data = json_decode($token, true);

                $user = User::where('email', $request->username)->with('role.rules')->get();

                $rules = $user->pluck('role')->collapse()->pluck('rules');

                $rules->put('access_token', $data['access_token']);

                return response()->json($rules);
                } catch (GuzzleHttpExceptionBadResponseException $e) {
                if ($e->getCode() === 400) {
                return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
                } else if ($e->getCode() === 401) {
                return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
                }
                return response()->json('Something went wrong on the server.', $e->getCode());
                }
                }





                share|improve this answer












                I was recommended the following discussion and it solved my issue. here is the final code



                public function login(Request $request)
                {
                $http = new GuzzleHttpClient;

                try {
                $response = $http->post(config('services.passport.login_endpoint'), [
                'form_params' => [
                'grant_type' => 'password',
                'client_id' => config('services.passport.client_id'),
                'client_secret' => config('services.passport.client_secret'),
                'username' => $request->username,
                'password' => $request->password,
                ]
                ]);

                $token = $response->getBody();

                $data = json_decode($token, true);

                $user = User::where('email', $request->username)->with('role.rules')->get();

                $rules = $user->pluck('role')->collapse()->pluck('rules');

                $rules->put('access_token', $data['access_token']);

                return response()->json($rules);
                } catch (GuzzleHttpExceptionBadResponseException $e) {
                if ($e->getCode() === 400) {
                return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
                } else if ($e->getCode() === 401) {
                return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
                }
                return response()->json('Something went wrong on the server.', $e->getCode());
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 23:12









                TJ WeemsTJ Weems

                14219




                14219






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419399%2fis-there-a-way-to-include-data-with-http-guzzle-response-in-laravel%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'