Category Archives: afnetworking-2

POST application/x-www-form-urlencoded to Server with AFNetworking 2.0

I'm trying to post data to a server which successfully connects, but does not take the data I'm passing through the parameter. The server is set to take application/x-www-form-urlencoded. I looked up some examples on here, but did not see a fix for it.

My parameter looks like this:

parameter = @{@ "key1" : value1,
                "key2" : value2};

and my method is this:

- (void)postDataToServer:(NSDictionary*)parameters{

    NSString *URLString = @"http://urlInfo";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/x-www-form-urlencoded"];

    [manager POST:URLString
       parameters:parameters
          success:^(AFHTTPRequestOperation *operation, id responseObject)
        {
        NSLog(@"responseObject: %@", responseObject);
        NSLog(@"operation.responseString: %@",operation.responseString);
        NSLog(@"operation.response: %@",operation.response);

    }
          failure:
     ^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);
         NSLog(@"Error Response --> %@",operation.responseString);
     }];
}

My responses from the success block are as follows:

responseObject: <>
operation.responseString: 
operation.response: <NSHTTPURLResponse: 0x37cd60> { URL: http://urlInfo } { status code: 200, headers {
    "Content-Length" = 0;
    Date = "Mon, 24 Nov 2014 03:06:22 GMT";
    Server = "Apache-Coyote/1.1";
} }

Can someone please point out what is missing here? Thank you.