0

I'd like to search my persons by query. Everything is alright with my server side of work. I verified in postman that link/persons/query is working well of course when I add ?name=aaa it works well. But the problem is in my symfony code, when I click into Search button it gave me the list of all persons like if nothing is working so I discovered after dumping variable that the problem is the variable body that I use I tried to change it but it does not work. Here is my code:

PersonController.php

  public function listAction(Request $request) {

    $serializer = new Serializer(
      array(new GetSetMethodNormalizer(), new ArrayDenormalizer()),
      array(new JsonEncoder())
    );
    $headers = array('Accept' => 'application/json');
    $response = Unirest\Request::get(link/persons/',$headers);
    $person = $serializer->deserialize($response->raw_body,
     Person::class, 'json');
    $form = $this->createForm(PersonType::class, $person);

    if ($request->isMethod('POST')) {
      $form->handleRequest($request);
      $headers = array('Accept' => 'application/json');
      //$body = json_encode($person);
      $body = Unirest\Request\Body::multipart($person);
      //$body = serialize($person);
      dump($body);
      $response = Unirest\Request::get('link/persons/query',
      $headers,$body);
      //$response = Unirest\Request::get('link/persons
      // /query?firstName=aaa'); (this works well)
      dump($response->body);
        return $this->render('AppBundle:Person:PersonList.html.twig',
      array (
            'form' => $form->createView(),
            'persons' => $response->body,
            ) );
    }

    $response = Unirest\Request::get('link/persons/',$headers);
    //$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
    if ($response->body == null) {
      return $this->render('AppBundle:Person:PersonList.html.twig', 
         array (
           'form' => $form->createView(),
           'persons' => $response->body,
           ) );
    }

    return $this->render('AppBundle:Person:PersonList.html.twig',
     array (
         'form' => $form->createView(),
         'persons' => $response->body,
         ) );
   }

Otherwise, here is the interesting part of my file Person.html/Twig

<form novalidate="novalidate" method="post">
    {{ form_row(form.name) }}
    {{ form_rest(form) }}
    <div class="form-group col-md-offset-5">
        <button type="submit" class="btn btn-default">Search</button>
    </div>
</form>

So when I click into Search, the function "listAction" is executed but the problem exactly is that my $body is not the good one.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
bsm
  • 81
  • 2
  • 9

1 Answers1

0

Finally, I found the answer. I had to serialize and deserialize my datas. So I had just to replace:

$body =  $serializer->serialize($person, 'json');

By:

$var =  $serializer->serialize($person, 'json');
$body = Json_decode($var , true);

The problem was that I didn't convert my JSON to arrays that's what Json_decode function do.

bsm
  • 81
  • 2
  • 9