AFNetworking 2.0 Send Post Request with array of dictionary Parameters

倾然丶 夕夏残阳落幕 提交于 2020-02-06 06:52: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.



来源:https://stackoverflow.com/questions/35836886/afnetworking-2-0-send-post-request-with-array-of-dictionary-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!