Zend framework 2 : not able to send desired status code in response

4.1k Views Asked by At

I am building REST API on Zend Framework 2. I want to send certain status code in response whenever any error has occurred.

I tried below in my controller :

$statusCode = 401;
$this->response->setStatusCode($statusCode);
return new JsonModel(array("error message" => "error description"));

Echoing status code prints 401, but client-side application gets status code 200 every time.

How can I set status code to particular value?

Module.php :

class Module 
{
    public function getAutoloaderConfig()
    {
        return array(
                     'Zend\Loader\ClassMapAutoloader' => array(
                                                               __DIR__ . '/autoload_classmap.php',
                                                               ),
                     'Zend\Loader\StandardAutoloader' => array(
                                                               'namespaces' => array(
                                                                                     __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                                                                                     ),
                                                               ),
                     );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

}

EDIT : Below is how the response looks : enter image description here

2

There are 2 best solutions below

4
sroes On

Try this:

$statusCode = 401;
$response = $this->response;
$response->setStatusCode($statusCode);
$response->setContent(new JsonModel(array("error message" => "error description")));
return $response;

If this does not work, there might be an onDispatch event handler which overwrites the status code again. Check your Module.php's.

0
Rob Allen On

I've just tested this with a simple example project at https://github.com/akrabat/zf2-api-response-code

In a controller that extends AbstractRestfulController, you can certainly use

<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;

class IndexController extends AbstractRestfulController
{
    public function create($data)
    {
        $this->response->setStatusCode(401);
        return new JsonModel(array("message" => "Please authenticate!"));
    }
}

Testing via curl, I get this:

$ curl -v -X POST http://zf2-api-example.localhost/
* Adding handle: conn: 0x7f880b003a00
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f880b003a00) send_pipe: 1, recv_pipe: 0
* About to connect() to zf2-api-example.localhost port 80 (#0)
*   Trying 127.0.0.1...
* Connected to zf2-api-example.localhost (127.0.0.1) port 80 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.30.0
> Host: zf2-api-example.localhost
> Accept: */*
>
< HTTP/1.1 401 Unauthorized
< Date: Fri, 17 Jan 2014 10:02:47 GMT
* Server Apache is not blacklisted
< Server: Apache
< Content-Length: 34
< Content-Type: application/json; charset=utf-8
<
* Connection #0 to host zf2-api-example.localhost left intact
{"message":"Please authenticate!"}

As you can see, the response code set is 401. If you're not getting this with your application, please check my example on github and see if that one works with your server.