Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel http response with cookie

use GuzzleHttpClient;
use GuzzleHttpCookieCookieJar;
use IlluminateSupportFacadesHttp;

....
  
// Get the token generated when you are logged
public function getToken(){
    $response = Http::withHeaders([
      'login' => 'login',
      'password' => 'password'
    ])->post('/urlOfConnection');

    return $response->cookies()->getCookieByName('cookieName');
}

// Get data from api with cookie
public function getDataFromApi(){
    $jwt = $this->getToken();

    $cookieJar = CookieJar::fromArray([
      $jwt->getName() => $jwt->getValue()
    ], $jwt->getDomain());

    $req = new Client();
    $res =  $req->request('GET', '/getDataFromApi',
                          ['cookies' => $cookieJar]);
    return $res->getBody();
}
 
PREVIOUS NEXT
Tagged: #laravel #http #response #cookie
ADD COMMENT
Topic
Name
4+5 =