0

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?

2 Answers 2

0

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 .

2
  • Hi @Alex Paliarush, Thanks for the answer. Just I want to know how we can send the above-mentioned error code from our custom rest api call(Post/Get)? Can you please show me the example?
    – Manish
    Commented Mar 28, 2017 at 5:46
  • 1
    Hi using this magento.stackexchange.com/questions/83852/…
    – Manish
    Commented Mar 28, 2017 at 6:21
2

You can try send a custom http code in your service class using ResponseInterface.

Here a little example:

  1. Define your service interface like this:
<?php

namespace Test\Module\Api;

interface TestApiInterface
{
    /**
    * @api
    *
    * @return \Magento\Framework\App\ResponseInterface
    */
    public function foo();
}

Now on your service class definition

<?php

namespace Paymentez\Module\Model;

use Test\Module\Api\TestApiInterface;
use Magento\Framework\App\{
    RequestInterface,
    ResponseInterface
};

class TestApi implements TestApiInterface
{
    /**
    * @var \Magento\Framework\App\RequestInterface
    */
    protected $request;

    /**
    * @var \Magento\Framework\App\ResponseInterface
    */
    protected $response;

    /**
    * CustomerAddress constructor.
    * @param \Magento\Framework\App\RequestInterface $request
    * @param \Magento\Framework\App\ResponseInterface $response
    */
    public function __construct(RequestInterface $request,
        ResponseInterface $response)
    {
        $this->request = $request;
        $this->response = $response;
    }

    /**
    * @api
    *
    * @return \Magento\Framework\App\ResponseInterface
    */
    public function foo()
    {
        $rawContent = $this->request->getContent();
        $params = json_decode($rawContent, true);

        $response = $this->response;
        $response->getHeaders()->addHeaderLine('Content-Type', 'application/json');
        $response->setContent(json_encode([
            'status' => false,
            'request_params' => $params
        ]));

        $response->send();

        // Ugly trick 😢 for force this response
        die;
    }
}

Now you can see the use of die sentence, but this trick prevent the next override of http status code and headers on the finally response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.