Sunday, June 17, 2012

Json Implementation issues in Zend

I have spent much time on searching the Json service implementation in Zend framework.
I have tried all online help to get the Json response using Zend_Rest_Server and Zend_Json_Server but was unable to convert response to Json or get a response from the service.
So here is a post to save you all from the sample less Zend Framework.

Use Zend_Json_Server to make your service based on Json and don't follow sample with Zend_Rest_Router and Zend_Rest_Controller to convert Rest response to Json as much of my time was wasted on implementing this way.

But Zend_Json_Server will never give you response and you will never know why in any way.
Zend_Json_Server will always give response with header 204 No Content.
Its a hidden logic inside the Zend_Json_Server and Zend_Json_Server_Response_Http code Which I have to explore to get the response and here is the details.

Put echo before Handle method to get the result in response.
Here is code for a Registration class with signup method


        $service =new Zend_JSON_Server();
        $service->setClass("Registration");
        echo $service->handle();


Use RestClient extension in browser for creating Json request and  testing Json service.

You are getting the 204 No Content header due to an Id validation in Zend_Json_Server_Response_Http due to following code


if (!$this->isError() && (null === $this->getId())) {
            header('HTTP/1.1 204 No Content');
            return;
        }

So you have to put an Id in Json Request.
Here is the sample request.
Paste it in rest client and set method to be invoked and send Post Request and you will get a response


{
"method":"signup",
"Id":2,
"Params":{
"user":"test"
"email":"abc@def.com",
}
}



And here is the response screenshot with result and Id parameters.



Note: If you are developing a business critical services do consider the Microsoft  WCF framework.


No comments:

Post a Comment