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

0

Issue

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!

Solution

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

Answered By – WiatroBosy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More