title: FOSRestBundle setup for return JSON but still asking for Twig template
body: <p>I have configured FOSRestBundle as following:</p>
  
  <pre><code>#FOSRestBundle
  fos_rest:
      param_fetcher_listener: true
      body_listener: true
      format_listener:
          rules:
              - { path: ^/, priorities: [ json, html ], fallback_format: ~, prefer_extension: true }
          media_type:
              version_regex: '/(v|version)=(?P&lt;version&gt;[0-9\.]+)/'
  
      body_converter:
          enabled: true
          validate: true
  
      view:
          mime_types:
              json: ['application/json', 'application/json;version=1.0', 'application/json;version=1.1']
          view_response_listener: 'force'
          formats:
              xml:  false
              json: true
          templating_formats:
              html: true
  
      exception:
          codes:
              'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
              'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
          messages:
              'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
      allowed_methods_listener: true
      access_denied_listener:
          json: true
  </code></pre>
  
  <p>And I have this at controller:</p>
  
  <pre><code>namespace PDI\PDOneBundle\Controller\Rest;
  
  use FOS\RestBundle\Controller\FOSRestController;
  use Nelmio\ApiDocBundle\Annotation\ApiDoc;
  use FOS\RestBundle\Controller\Annotations\QueryParam;
  use FOS\RestBundle\Controller\Annotations\Get;
  
  class RepresentativeRestController extends FOSRestController
  {
      /**
       * Get all representatives.
       *
       * @return array
       *
       * @ApiDoc(
       *   resource = true,
       *       https = true,
       *   description = "Get all representatives.",
       *   statusCodes = {
       *      200 = "Returned when successful",
       *      400 = "Returned when errors"
       *   }
       * )
       * @Get("/api/v1/reps")
       */
      public function getRepsAction()
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $entities = $em-&gt;getRepository('PDOneBundle:Representative')-&gt;findAll();
  
          if(!$entities)
          {
              return $this-&gt;view(null, 400);
          }
  
          return $this-&gt;view($entities, 200);
      }
  }
  </code></pre>
  
  <p>But when I try the following URL <code>app_dev.php/api/v1/reps</code> I got this error:</p>
  
  <blockquote>
    <p>Unable to find template "". 500 Internal Server Error -
    InvalidArgumentException 3 linked Exceptions: Twig_Error_Loader »
    InvalidArgumentException » InvalidArgumentException »</p>
  </blockquote>
  
  <p>I expect that API return a well formed JSON as the following example:</p>
  
  <pre><code>{
     "id":"30000001",
     "veeva_rep_id":"0055648764067SwzAAE",
     "display_name":"John Know",
     "avatar_url":"http://freelanceme.net/Images/default%20profile%20picture.png",
     "rep_type":"VEEVA",
     "username":"john@mail.com",
     "first":"John",
     "last":"Know",
     "title":"Sales Representative",
     "phone":"800-555-1212",
     "email":"john@mail.com",
     "territory_id":"200454001",
     "inactive":"no",
     "total_contacts":"6",
     "total_shares":"0",
     "totalViews":"0",
     "lastLoginAt":"2015-05-05 15:45:57",
     "lastVeevaSyncAt":"2015-05-05 15:45:57",
     "createdAt":"2015-05-05 15:45:57",
     "updatedAt":"2015-05-05 15:45:57"
  }
  </code></pre>
  
  <p>Is not FOSRestBundle configured for return JSON? Why still asking for Twig template? How can I fix this?</p>
  
  <p><strong>First test:</strong></p>
  
  <p>As @Jeet suggest me I have tried using Postman (is the same as the extension he told me) and after set the header <code>Content-Type</code> to <code>application/json</code> the error turns into this </p>
  
  <blockquote>
    <p>Malformed JSON</p>
  </blockquote>
  
  <p>so, the FOSRestBundle is not setting up headers as should be and controller is not returning a valid JSON, how do I fix those ones?</p>
  
  <p><strong>Second test:</strong></p>
  
  <p>As suggested by <strong>@Jeet</strong> I run this test:</p>
  
  <pre><code>/**
   * Get all representatives.
   *
   * @return array
   *
   * @ApiDoc(
   *   resource = true,
   *       https = true,
   *   description = "Get all representatives.",
   *   statusCodes = {
   *      200 = "Returned when successful",
   *      400 = "Returned when errors"
   *   }
   * )
   * @Get("/api/v1/reps")
   * @View()
   */
  public function getRepsAction()
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
      $entities = $em-&gt;getRepository('PDOneBundle:Representative')-&gt;findAll();
  
      $temp = array("1", "2", "3");
  
      $view = $this-&gt;view($temp, Codes::HTTP_OK);
      return $this-&gt;handleView($view);
  }
  </code></pre>
  
  <p>And still the same issue:</p>
  
  <blockquote>
    <p>Unable to find template "". 500 Internal Server Error -
    InvalidArgumentException 3 linked Exceptions: Twig_Error_Loader »
    InvalidArgumentException » InvalidArgumentException »</p>
  </blockquote>
  
  <p>What else can be wrong here? Did I'm missing something at configuration? </p>
  
  <p>I forgot to add <code>app/config/routing.yml</code> and <code>src/PDI/PDOneBundle/Resources/config/routing.yml</code> at first so here them goes, perhaps this is the missing piece on the puzzle and give you a better idea of where the problem comes from:</p>
  
  <pre><code>#app/config/routing.yml
  #PDOne
  pdone:
      resource: "@PDOneBundle/Resources/config/routing.yml"
  
  template:
      resource: "@TemplateBundle/Resources/config/routing.yml"
  
  #FOSUserBundle
  fos_user:
      resource: "@FOSUserBundle/Resources/config/routing/all.xml"
      prefix: /
  
  #NelmioApiDocBundle:
  NelmioApiDocBundle:
      resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
      prefix:   /api/doc
  
  #SonataAdmin
  admin:
      resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
      prefix: /admin
  
  _sonata_admin:
      resource: .
      type: sonata_admin
      prefix: /admin
  
  #src/PDI/PDOneBundle/Resources/config/routing.yml
  pdone:
      resource: "@PDOneBundle/Controller/"
      type:     annotation
      prefix:   /
  </code></pre>
  
  <p><strong>Third test:</strong></p>
  
  <p>Definitely something is wrong with request from client side, if I use a tool like <code>Postman</code> and set proper headers I got the entities as I want, see pic below:</p>
  
  <p><img src="https://i.stack.imgur.com/X4Bsp.png" alt="enter image description here"></p>
  
  <p>I can't find where the problem is so I desperately need someone's help here because I was already out of ideas</p>