Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting JSON data using AFNetworking 2.0

I have a web script which accepts JSON string as input through HTTP POST request. I have came across several AFNetworking 1.x example for the same , can anybody please point me or give AFNetworking 2.0 example to do an HTTP POST request to a web script with formatted JSON as input ?

Thanks

like image 299
vishal dharankar Avatar asked Jan 31 '14 18:01

vishal dharankar


4 Answers

after searching docs and trying out some codes I got following as an example

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];


NSDictionary *params = @ {@"user" :txtUserName, @"pwd" :txtPwd };


[manager POST:URL_SIGNIN parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
    NSLog(@"JSON: %@", responseObject);
}
failure:
 ^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", error);
 }];

Also don't forget to set response header type in the server script as Application/json.

like image 186
vishal dharankar Avatar answered Oct 16 '22 20:10

vishal dharankar


Here is some simple template for POST parameters stored in NSMutableDictionary parameters in JSON format. Works with AFNetworking 2.4.1.

NSString *baseURL = @"http://your-server.com/";
NSString *path = @"method/url/";

NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@"value" forKey:@"key"];

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager POST:path parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {

        NSLog(@"JSON: %@", responseObject);
        //here is place for code executed in success case

} failure:^(NSURLSessionDataTask *task, NSError *error) {

        //here is place for code executed in error case
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
                                                            message:@"Sorry, try again."
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];

        NSLog(@"Error: %@", [error localizedDescription]);
}];
like image 39
lukszar Avatar answered Oct 16 '22 21:10

lukszar


If you want to post json to server, you should post your parameters use this way below.

Use this way , you can then find out the "Content-Type" in your request header is "application/json".

AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = serializer;

 NSDictionary *paras = @{@"uid" : @(10020)};

[manager POST:@"http://your.request.url" parameters:paras success:^(AFHTTPRequestOperation *operation, id responseObject) {



}failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"the falire is %@", error);

}];

May this help. :)

like image 8
generalzyq Avatar answered Oct 16 '22 20:10

generalzyq


In swift:

Add this in manager.AFHTTPRequestOperationManager

manager.requestSerializer = AFJSONRequestSerializer(writingOptions: NSJSONWritingOptions.PrettyPrinted)
like image 5
Tamojit Pal Avatar answered Oct 16 '22 20:10

Tamojit Pal