Search code examples
iosobjective-cbase64afnetworking-2url-encoding

AFNetworking 2.0: How to pass complete json param argument converted as base64encoded + urlencoded string for AFNetworking 2.0 Post Request


I am using AFNetworking 2.0 & new to it. Tried couple of sample WS calls with success. We have Web Service implemented & to be called like:

It's data parameter is encrypted string.

Request is: http://demo.XYZ.net/getlanguage // Just for example

Parameters:

1) first you need to crate json with parameter like below dictionary to string

   {
       param =     {
           pone = "com.xyz";
           ptwo = 68208;
           pthree = eda24e95f;
       };
   }

to

   {"param":{"pone":"com.xyz","ptwo":"68208","pthree":"eda24e95f"}}

2) and then convert json string into the base64encode

   something like: eyJob21lIjp7InNGFlMGEzZDg1Mzg3YTNkYmFlZDQ5MzBiMCIsInNhbHQiOiI3OTU2IiwicGFj5uY19

3) and then convert string into the urlencode then send encrypted string in "data" with post method

eyJob21lIjp7InNpZ24iOiJiZmU4Y2RmZGEzZDg1Mzg3YTNkYmFlZDQ5MzBiMCIsInNhbHQi%0AOiI3OTU2IiwicGFja2FnZSI6ImNvbS5uYmJk

Then Finally Pass that encoded string as below:

http://demo.XYZ.net/getlanguage?data=eyJob21lIjp7InNpZ24iOiJiZmU4Y2RmZGEzZDg1Mzg3YTNkYmFlZDQ5MzBiMCIsInNhbHQi%0AOiI3OTU2IiwicGFja2FnZSI6ImNvbS5uYmJk

Above WS is perfectly working.[Checked on "POSTMan-Chrome-Extension"]

The question is how to implement same with AFNetworking 2.0 ? where to encrypt using params like above when we use AFHTTPRequestOperationManager?

Thanks


Solution

  • If this is really what your server requires, you can simply convert the original param dictionary to JSON using NSJSONSerialization and then base64 encode it with base64EncodedStringWithOptions. Having done that, you can build your new parameters dictionary with that string:

    NSString *urlString = @"http://demo.XYZ.net/getlanguage";
    
    NSDictionary *originalParameters = @{@"param" : @{
                                                 @"pone"   : @"com.xyz",
                                                 @"ptwo"   : @68208,
                                                 @"pthree" : @"eda24e95f"
                                                 }
                                         };
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:originalParameters options:0 error:&error];
    NSAssert(jsonData, @"JSON encoding failed: %@", error);
    
    NSString *base64EncodedString = [jsonData base64EncodedStringWithOptions:0];
    
    NSDictionary *parameters = @{@"data" : base64EncodedString};
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:urlString parameters:parameters success:^(NSURLSessionDataTask * __nonnull task, id __nonnull responseObject) {
        NSLog(@"responseObject = %@", responseObject);
    } failure:^(NSURLSessionDataTask * __nonnull task, NSError * __nonnull error) {
        NSLog(@"error = %@", error);
    }];
    

    If you're using AFHTTPRequestOperationManager, the basic idea is the same: Convert dictionary of parameters to JSON, base64 encode it, and then create new parameter dictionary for that and supply it to the GET method.