You can try send a custom http code in your service class using ResponseInterface
.
Here a little example:
- 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