0

I'm using Symfony to build REST APIs. I'm in a situation where I want to do some actions in the kernel.terminate event, but I want to make sure that the request was successfully processed before doing so.

For that, as I use REST conventions and should return an appropriate HTTP status code when an error occured, I want to check the response HTTP status. The method Symfony\Component\HttpFoundation\Response::isOk() seems appropriate and more readable than checking manually if the HTTP status is between 200 and 299, but it returns true only if the HTTP status equals 200. As I return a 201 status code when a resource is created, I can't use it for that.

Before opening an issue on Github, I was wondering if there is a reason for this method to not return true for other success HTTP statuses?

Thanks!

Boulzy
  • 446
  • 4
  • 11

1 Answers1

2

You can use the isSuccessful() method from the Response object:

/**
 * Is response successful?
 *
 * @return bool
 *
 * @final since version 3.2
 */
public function isSuccessful()
{
    return $this->statusCode >= 200 && $this->statusCode < 300;
}
Hammerbot
  • 15,696
  • 9
  • 61
  • 103
  • Thanks, my bad for I didn't even look further to search for this method and now realize that the `isOk()` method is related to the name of the HTTP status code. Thank you for your help! – Boulzy Sep 28 '18 at 12:15
  • You're welcome, I'm always happy to provide help for a fellow Euratech Colleague ;) – Hammerbot Sep 28 '18 at 12:16