AFNetworking 2.0 Send Post Request with array of d

2019-08-12 00:28发布

问题:

I want to try post parameter in the following API but the parameter is not getting passed properly, and the response received gives the message that, data is required. So can anyone please help to sort out this problem.

my url is

forapp.com/api/getContacts.php?data=[ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]

so how i can pass this type of request to the api

   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"name": @"hello",
                             @"phone": @"1234567890"};
    NSLog(@"Dict %@",params);
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

[manager POST:@"http://forapp.com/api/getContacts.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);

}];

回答1:

just add

Step-1

// create the dictionary of {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} this

NSDictionary *params = @{@"name": @"hello",
                         @"phone": @"1234567890"};

Step-2

//create the one array of this output [ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]

NSMutableArray *arr = [NSMutableArray arrayWithObjects:params,nil];

Step-3

// convert your object to JSOn String
NSError *error = nil;
NSString *createJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart
                                                                                options:NSJSONWritingPrettyPrinted
                                                                                  error:&error]
                                       encoding:NSUTF8StringEncoding];

Step-4

//create the another one dictionary for send the data for like data=[ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]

 NSDictionary *pardsams = @{@"data": createJSON};

Step-5

  // finally start your request
  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSLog(@"Dict %@",pardsams);
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

[manager POST:@"http://forapp.com/api/getContacts.php" parameters:pardsams success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);

}];


回答2:

as per this url

forapp.com/api/getContacts.php?data=[ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ]

You should implement like this

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"name": @"hello",
                             @"phone": @"1234567890"};
    NSLog(@"Dict %@",params);
manager.responseSerializer = [AFJSONResponseSerializer serializer];

manager.requestSerializer = [AFJSONRequestSerializer serializer];

    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

[manager POST:@"http://forapp.com/api/getContacts.php" parameters:@{@"data": @[params]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);

}];


回答3:

Add following:

// httpClient is a subclass of AFHTTPRequestOperationManager or AFHTTPSessionManager
httpClient.requestSerializer = AFJSONRequestSerializer;

And also check your post array does not include any other object. It just include array.