0

I am developing a simple REST API for an application using Symfony and JMS Serializer Bundle. I have a few URLs - for example /api/answers/{id}. Now the problem is if I call that URL with id=1, everything works like a charm, when I call it with id=2, 3, 4... the JSON content is encoded improperly.

The backend script looks like this (It's just a prototype I will refactor it):

    public function getByQuestionIdAction($questionId)
    {
         $answers = $this->getDoctrine()->getRepository('B2GameBundle:Answer')->findByQuestion($this->getDoctrine()
                                        ->getRepository('B2GameBundle:Question')->find($questionId));

         $serializer = $this->get('jms_serializer');

        if(!empty($answers))
        {
            return Response::create($serializer->serialize($answers, 'json'), 200, array("Content-type" =>     "application/json"));
        }
        else
        {
            return Response::create(json_encode(array('error' => 'No answers for question with id ' .     $questionId . ' found!')), 404, array("Content-type" => "application/json"));
        }

    }

All my database tables are set to utf8_unicode_ci, every bit in the configuration is set to utf8 as well.

PS: I printed the $answers variable with print_r and it's improperly encoded as well, so I suppose the problem lies at the doctrine side of thing. What do you think? What is really strange is the fact that it works fine for id=1. Mind you I look inside phpmyadmin and the text is just fine too.

Thank you.

michy04
  • 317
  • 3
  • 11
  • On which OS do you develop? And on which one is the server running? – ferdynator Feb 07 '14 at 17:35
  • The OS I use for development is Windows. Server is local (WAMP). – michy04 Feb 07 '14 at 17:46
  • (Windows Notepad issue) Please, consult this, I shared the problem too and it fixed it: http://stackoverflow.com/questions/10290849/how-to-remove-multiple-utf-8-bom-sequences-before-doctype – Felix Aballi Sep 19 '14 at 14:53

1 Answers1

0

I solved it. The problem was the encoding on the page was different, even though I set it to UTF8 in Symfony. I had to set it manually in the Content-Type header to make it work.

michy04
  • 317
  • 3
  • 11