title: Angular JS $http.post method malforms JSON arrays
body: <p><strong>Hi SO.</strong></p>
  
  <p>I have a weird problem that I've been struggling with all day long.</p>
  
  <p>I have my Angular JS module (App), and a controller. In that controller, I inject a factory, which handles my $http.post method. (Which i call "doSearch"). So far so good.</p>
  
  <p>On the back-end I have a spring-mvc rest api class. It looks like this:</p>
  
  <pre><code>@Controller
  @RequestMapping("/api/filesearch")
  public class FileSearchAPI {
  
      @RequestMapping(value = "/search", method = RequestMethod.POST)
      @ResponseBody
      public ResultDTO doSearch(@RequestBody NewPerfectSearchDTO searchDTO) {
            System.out.println(searchDTO.getIdentifier());
            SolrService solrService = new SolrService();
            //return solrService.query(searchDTO);
            return new ResultDTO();
       }
  }
  </code></pre>
  
  <p>And this is the function that sends some JSON, that will be mapped to a JAVA POJO/DTO:</p>
  
  <pre><code>doSearch: function(searchParams) {
          $http.post("/np/api/filesearch/search", JSON.stringify({identifier: "apa", inputRows: []})).success(function(response) {
              console.log(response);
              return response;
          }).error(function(data, status, headers, config) {
                  console.log(status, headers, config);
          });
      }
  </code></pre>
  
  <p>Take note of the payload (Yes, it's static for this example):
  <code>JSON.stringify({identifier: "apa", inputRows: []})</code></p>
  
  <p>This maps perfectly fine in my DTO. The fields are correct, and variable <code>inputRows</code> is an empty, initialized array.</p>
  
  <p><strong>HOWEVER</strong> when I look at the payload (Using firebug or the chrome inspector) , sent by the client, to the API,
  it looks like this: <code>{"identifier":"apa","inputRows":"[]"}</code></p>
  
  <p>Did you notice the <code>""</code> around my JSON array? </p>
  
  <pre><code>{"identifier":"apa","inputRows":"[]"}
                                  ^  ^
                                  |  |
  </code></pre>
  
  <p>Yeah... That's the problem I'm having.
  When I send that request, the server simply replies with a <code>HTTP 400</code>, malformed syntax (Tomcat/Spring error page), the request doesn't even invoke the APIController on the back-end.</p>
  
  <p>So, what I've tried so far (without success):</p>
  
  <ul>
  <li>I have tried without using <code>JSON.stringify()</code> on the payload, with
  the same result</li>
  <li>I have tried specifying the headers in my APIController (No use, it doesn't even execute that code before throwing the error)</li>
  <li>I have tried using a <code>Java.util.List</code> instead of an array in the DTO</li>
  </ul>
  
  <p>So I'm pretty much stuck here. Any help would be appreciated. </p>