Search code examples
jsonsymfonyexceptioncodenameoneencode

Control character error, possibly incorrectly encoded


I'm working on Mobile application with codenameone linked to symfony 4, user can participate to contest of videos i have this tables users,participation,video,concour everything is working very well but when i try to participate it brings me all the data correcty but it shows me this error: Control character error, possibly incorrectly encoded i hope that someone could help Me thank you. This is my fonction on symfony4 :

     *
     *
     * @Route("/api/competition/participate/", name="api_competitions_participate")
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function participateAction(Request $request)
    {


        $r=$request->query->get('video');

        $encoders = [new XmlEncoder(), new JsonEncoder()];
        $normalizers = [new ObjectNormalizer()];

        $serializer = new Serializer($normalizers, $encoders);
        $video = $serializer->deserialize($r, Video::class, 'json');
        $u=$this->getDoctrine()->getRepository(Users::class)->find($video->getOwner()['id']);
        $video->setOwner($u);
        $video->setPublishdate(new \DateTime('now'));
         dump($video);

        $r2=$request->query->get('participation');
        $participation= $serializer->deserialize($r2, Participation::class, 'json');
        $c=$this->getDoctrine()->getRepository(Concour::class)->find($participation->getConcour()['id']);
        $participation->setUser($u);
        $participation->setConcour($c);
        $participation->setVideo($video);
        $participation->setDateParticipation($video->getPublishdate());
         dump($participation);
        $em = $this->getDoctrine()->getManager();
        $em->persist($video);
        $em->persist($participation);
        $em->flush();
        dump($r);
        
        return new JsonResponse();

    }

and this is the function on codenameone

   public void participate(Video v, Participation cp) {
        Gson gson = new Gson();
        String json = gson.toJson(v);
        String json2 = gson.toJson(cp);
        String url = Statics.BASE_URL + "/api/competition/participate/?video=" + json + "&participation=" + json2;

        con.setUrl(url);

        con.addResponseListener(new ActionListener<NetworkEvent>() {
            @Override
            public void actionPerformed(NetworkEvent evt) {
                String str = new String(con.getResponseData());
                System.out.println(str);
                Dialog.show("Confirmation", "Your Video has been successfully added", "Ok", null);
                con.removeResponseListener(this);
            }

        });
        NetworkManager.getInstance().addToQueueAndWait(con);

Solution

  • I'm not familiar with Symfony so I'm not sure if this is the right answer. Also your approach of passing JSON in a get query is considered bad form by most. Normally JSON would be passed in the POST method body for REST calls.

    To properly encode use this:

    String url = Statics.BASE_URL + "/api/competition/participate/"
    con.setUrl(url);
    con.addArgument("video", json);
    con.addArgument("participation", json2);
    

    Also make sure the ConnectionRequest is constructed with false to indicate this is a GET operation and not POST.