Home > Blockchain >  What are the differences between ok(), successful() and failed() in Laravels guzzlehttp/guzzle?
What are the differences between ok(), successful() and failed() in Laravels guzzlehttp/guzzle?

Time:12-16

What are the differences between these three Guzzle (guzzlehttp/guzzle) methods:

use Illuminate\Support\Facades\Http;

$response = Http::get('http://example.com');
  1. $response->ok() : bool;
  2. $response->successful() : bool;
  3. $response->failed() : bool;

And when does it make sense to use one of these methods in preference to the other?

Thanks in advance!

CodePudding user response:

These methods come from Laravel

public function successful() { return $this->status() >= 200 && $this->status() < 300; }

public function ok() { return $this->status() === 200; }

public function failed() { return $this->serverError() || $this->clientError(); }

and the differences can be seen with the naked eye

successful() -return true when staus is bettwen 200 , 300

ok() - return true only when status = 200

failed() - return bool if any error

And when does it make sense to use one of these methods in preference to the other?
It depends on the code and the need

  • Related