3

I have a RESTful Zend action which should send me back a json encoded object, but in the response whatever I set in the body gets duplicated.

My code looks like this :

    public function blablaAction() {
      $this->_helper->viewRenderer->setNoRender();
      $response = $this->getResponse();
      [...]
      $response->setBody('aaaaaaaa' . json_encode($output) . 'aaaaaaaa');
      $response->sendResponse();
    }

And if I look at the response body, I can see:

      aaaaaaaaXXXXXXXXXXXXXXaaaaaaaaaaaaaaaaXXXXXXXXXXXXXXaaaaaaaa

(XXXXXXXXXXXXXX being the json encoded data).

Why?

PS: I added the aaaaaaa just to make sure the problem didn't come from the json encoding. I'll just have $response->setBody(json_encode($output)); when it finally works as expected.

Shautieh
  • 746
  • 10
  • 17

1 Answers1

0

I just found a workaround from this question: Zend response application/json utf-8

It works as expected if, instead of using $response along with setBody, I use $this->_helper->json->sendJson($output);.

This is only a workaround and would like to understand what's the problem with setBody, so I won't accept this "answer"...

Community
  • 1
  • 1
Shautieh
  • 746
  • 10
  • 17