RSS Feed

AFNetworking : How to post data (NSData) on server.

Posted on

Few of my friends are facing issue in uploading JSON data on server. I am writing this post for those developer who are new in AFNetworking and unable to upload JSON data on there web server.

1. Create your parameter dictionary as : 
 NSDictionary *jsonDic = [NSDictionary dictionaryWithObjectsAndKeys

 value,         @”key”,

 value,         @”key”,

 value,         @”key”, // etc


2. Convert this dictionary into data : 

NSError *error = nil;

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:NSJSONWritingPrettyPrinted error:&error];

3. Now follow this to upload data on server :

// Create request body 

    NSMutableData *body = [NSMutableData data];

// Append of the body with your data

    [body appendData:jsonData];

        [request setHTTPBody:body];

// Set request method POST

     [request setHTTPMethod:@”POST”];

// Set request Headers

    [request setValue:@”application/json” forHTTPHeaderField:@”Accept”];

    [request setValue:@”application/json” forHTTPHeaderField:@”content-type”];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

 // Success

  NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

              NSDictionary *responseDict = [responseString JSONValue];

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

// Failure

        NSLog(@”Error: %@”, error);


      [operation start];


Cheers !!


About Muzammil

Over the past 6 years, I have designed, developed and managed over 150 iPhone/iPad applications for many large-scale clients, some include Rolex, Ford, Makemark LLC, Source Digital Production, IT City (Kingdom of Saudi Arabia), R.Ø.S.A. Creation etc. These applications ranged from simple navigation bar application to high end user interface design for image processing, simple games with Core Graphics and OpenGL. Before started Triffort Technologies i was working in Ampere Software Pvt Ltd as a senior iOS Developer. I was the first employe of Ampere who started iPhone/iPad development in the company. In ampere i worked on some complex apps like Boxboss, Austin Post, User Tracking etc. Throughout my career, I have used Core Graphics, OpenGL, SQLite, Core Data, JSON, XML etc. I am very proficient in many design programs including Photoshop, Illustrator, Flash, iMove. Although I am very knowledgeable in all Adobe Products. Most the time i am designing my apps without taking help from any graphics designer. I love to make app from my designs. It feels great when you are creating app from scratch and no body else is involve in your ideas. You are free to do anything. As the Senior Developer and Team lead at Triffort Technologies, I managed a team of Technical Interactive Developers and Designers. As their manager I am responsible for scheduling, resource management, task reviews, process development and enhancements, employee reviews and new employee hiring. I believe good employee morale and communication are key factors to successful, creative work, therefore, positive feedback was part of my daily routine. But most of the time you can find me on my desk writing codes for some app. I love programming. This is my passion. I’m a team player. I am a motivated individual who enjoys taking on new challenges and learning new technologies. I am well organized and a proficient multi-tasker. I have strong problem solving skills such as the ability to use technology in creative ways to meet unique project requirements. I love to be challenged and I always have a positive outlook no matter how tight the deadline is. If you would like to contact me or find out more about my experience, please contact me at or you can Skype me at muz.iphonedev.

14 responses »

  1. request? What is it?

    • Here request is your NSMutableURLRequest :

      NSMutableURLRequest *request = [httpClient requestWithMethod:@”POST”

      *urlString is your web-service url.

  2. Also code mostly isnt working for last version of AFNetworking

  3. httpClient? what is it?

  4. Thanks Muzammil! This is the unique tutorial with POST request and JSON 100% working to AFNetworking 1.x

  5. I want to working with your code.Its show two error.
    Error 1: No visible @interface for ‘NSString’ declares the selector ‘JSONValue’
    Error 2: No visible @interface for ‘AFHTTPRequestOperationManager’ declares the selector ‘registerHTTPOperationClass:’

  6. Hiiiiii. Can you please look at this question..
    I just can’t find the solution .. it’s related to the uploading file NSData fetched from PHAsset

    • Are you uploading in main thread or background thread? Try to upload this in background thread. So it should not affect on others. Also make sure your app is configured to run in background mode.

  7. hiii.
    can you plz tell me, upload video path and data(like id ,name ) to server


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: