title: AFNetworking posts JSON arrays as multiple single-entry dictionaries
body: <p>I'm having a similar issue to the one discussed <a href="https://stackoverflow.com/questions/17475122/json-data-messed-up-in-afnetworking-post-request">here</a>.</p>
  
  <p>I'm attempting to post JSON to a server.</p>
  
  <p>Here's the Objective-C code that <em>should</em> work, but doesn't. I get an empty array in the response object, not sure why:</p>
  
  <pre><code>    AFHTTPRequestOperation * operation = [manager POST:uploadScriptUrl
        parameters:mutableJSON
        success:^(AFHTTPRequestOperation * operation, id responseObject) {
            successBlock(operation, responseObject);
        }
        failure:^(AFHTTPRequestOperation * operation, NSError * error) {
            failureBlock(operation, error);
        }];
  </code></pre>
  
  <p>This code (which is adapted from code I use to upload images) <em>KIND OF</em> works. I know it's definitely not the approved way to do it:</p>
  
  <pre class="lang-c prettyprint-override"><code>    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
      AFJSONRequestSerializer * requestSerializer = [AFJSONRequestSerializer serializer];
      [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
      [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
      manager.requestSerializer = requestSerializer;
  
      AFHTTPRequestOperation * operation = [manager POST:uploadScriptUrl
      parameters:mutableJSON //passed in
      constructingBodyWithBlock:^(id &lt;AFMultipartFormData&gt; formData) {
          //Do nothing
     }
      success:^(AFHTTPRequestOperation * operation, id responseObject) {
          successBlock(operation, responseObject);
      }
      failure:^(AFHTTPRequestOperation * operation, NSError * error) {
          failureBlock(operation, error);
      }];
  </code></pre>
  
  <p>It posts my JSON to the server, but in the process of formulating the request, the JSON is mangled.</p>
  
  <p>Here's how it starts out:</p>
  
  <pre><code>{
  "key1": [
      {
          "dictionary1": {
              "param1": "value1",
              "param2": "value2",
              "array1A": [
                  "value1",
                  "value2"
              ],
              "array1B": [
                  "value1",
                  "value2"
              ]
          }
      }
  ]
  </code></pre>
  
  <p>}</p>
  
  <p>and here's what AFNetworking sends to the server:</p>
  
  <pre><code>{
  "key1": [
      {
          "dictionary1": {
              "array1A": [
                  "value1"
              ]
          }
      },
      {
          "dictionary1": {
              "array1A": [
                  "value2"
              ]
          }
      },
      {
          "dictionary1": {
              "array1B": [
                  "value1"
              ]
          }
      },
      {
          "dictionary1": {
              "array1B": [
                  "value2"
              ]
          }
      },
      {
          "dictionary1": {
              "param1": "value1"
          }
      },
      {
          "dictionary1": {
              "param2": "value2"
          }
      }
  ]
  </code></pre>
  
  <p>}</p>
  
  <p>Here's what Charles shows for the request. You can see how the JSON structure has already been altered in the request, before the server has touched the data.</p>
  
  <p><img src="https://i.stack.imgur.com/x1Mnk.png" alt=""></p>
  
  <p>Here's the PHP I'm using on the server. Dead simple for now:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  header('Content-type: application/json'); //Not sure if this is needed.
  
  $json_string = json_encode($_POST);
  
  header("HTTP/1.0 200 OK");
  echo $json_string;
  
  ?&gt;
  </code></pre>
  
  <p>So, all of that said, here are my questions:</p>
  
  <ol>
  <li><p>Does AFNetworking handle nested JSON arrays? On <a href="https://github.com/AFNetworking/AFNetworking/issues/783" rel="nofollow noreferrer">this page</a> Mattt says: "The structure you're describing can't deterministically be represented with query string encoding." I'm using POST, so query strings are not involved. But maybe the limitation exists with POST data as well?</p></li>
  <li><p>I'm also curious why the longer AFNetworking call that includes <code>constructingBodyWithBlock</code> succeeds while the shorter one fails. However, this answer is less important to me. The longer method very nearly works and I'd be happy to use it if it returned the same JSON that I send.</p></li>
  </ol>
  
  <p>Thanks all! </p>