Magento 2 – How to Return a 304 HTTP Response from REST API Method

apimagento2rest

In a custom REST API endpoint method, returning the object promised in the service class interface will generate a 200 OK response. Additionally, I can return a specific HTTP error code by throwing an particular type of exception. For example, throwing a NoSuchEntityException returns a 404 NOT FOUND and a AuthorizationException returns a 401 UNAUTHORIZED.

My question: is there a way to return other HTTP codes, such as a 304 NOT MODIFIED code?

Best Answer

Custom status codes are currently not supported, you can only get one of the following: 200, 400, 401, 403, 404, 406, 500 (see \Magento\Framework\Webapi\Exception). If you have real use case when you need this functionality, feel free to create pull request or file an issue at https://github.com/magento/magento2 .