Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm having a similar issue to the one discussed here.

I'm attempting to post JSON to a server.

Here's the Objective-C code that should work, but doesn't. I get an empty array in the response object, not sure why:

    AFHTTPRequestOperation * operation = [manager POST:uploadScriptUrl
      parameters:mutableJSON
      success:^(AFHTTPRequestOperation * operation, id responseObject) {
          successBlock(operation, responseObject);
      }
      failure:^(AFHTTPRequestOperation * operation, NSError * error) {
          failureBlock(operation, error);
      }];

This code (which is adapted from code I use to upload images) KIND OF works. I know it's definitely not the approved way to do it:

    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 <AFMultipartFormData> formData) {
        //Do nothing
   }
    success:^(AFHTTPRequestOperation * operation, id responseObject) {
        successBlock(operation, responseObject);
    }
    failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        failureBlock(operation, error);
    }];

It posts my JSON to the server, but in the process of formulating the request, the JSON is mangled.

Here's how it starts out:

{
"key1": [
    {
        "dictionary1": {
            "param1": "value1",
            "param2": "value2",
            "array1A": [
                "value1",
                "value2"
            ],
            "array1B": [
                "value1",
                "value2"
            ]
        }
    }
]

}

and here's what AFNetworking sends to the server:

{
"key1": [
    {
        "dictionary1": {
            "array1A": [
                "value1"
            ]
        }
    },
    {
        "dictionary1": {
            "array1A": [
                "value2"
            ]
        }
    },
    {
        "dictionary1": {
            "array1B": [
                "value1"
            ]
        }
    },
    {
        "dictionary1": {
            "array1B": [
                "value2"
            ]
        }
    },
    {
        "dictionary1": {
            "param1": "value1"
        }
    },
    {
        "dictionary1": {
            "param2": "value2"
        }
    }
]

}

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.

Here's the PHP I'm using on the server. Dead simple for now:

<?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;

?>

So, all of that said, here are my questions:

  1. Does AFNetworking handle nested JSON arrays? On this page 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?

  2. I'm also curious why the longer AFNetworking call that includes constructingBodyWithBlock 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.

Thanks all!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
426 views
Welcome To Ask or Share your Answers For Others

1 Answer

I'm using POST, so query strings are not involved. But maybe the limitation exists with POST data as well?

The limitation is not on POST, it's URL form encoding. I go on to say that you should encode as JSON, which you can do with the following configuration change to your manager:

manager.requestSerializer = [AFJSONRequestSerializer serializer];.

Also, unless you're actually constructing a multi-part request, don't use the constructingBodyWithBlock version of that method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...