Today I will introduce you about Common Error HTTP Codes in Magento 2 Rest API. Firstly, you also need to know what HTTP is? HTTP stands for Hyper Text Transfer Protocol, the default protocol on the website. During using that protocol, maybe there are some common errors which are called as HTTP error codes.

The below includes a list of the codes and the meaning of each the common error HTTP code in Magento 2 Rest API for the better understanding.

HTTP Error 400

HTTP Error 401

HTTP Error 403

HTTP Error 404

HTTP Error 405

HTTP Error 406

HTTP Error 500

Next, I will give you the example to get some of the error HTTP codes.

If you run an exception in your API method:

if (!$productId) {
throw new \Magento\Framework\Exception\NoSuchEntityException(__('Requested product doesn\'t exist'));

At that time, the HTTP Error 404 will show in the response.

Or you can use exception classes which Magento provides in Magento\Framework\Exception, or use general exception class \Exception:

if (!$productId) {
throw new \Exception(__('Requested product doesn\'t exist'));

Ensure that the error code always is 500 (Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR).

You may get this error message: Exception printing is disabled by default for security reasons