Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AFHTTPRequestOperation to construct URL (curly braces included)

I need to construct URL like this using AFNetworking the problem for me it's { and } how to pass throught parameter

/api/sth.json?filter[condition]={"53891":[123],"53892":[123,124]}

So my code looks like this (i made it simpler):

[self GET:myUrl parameters:@{
                             @"filter" : @{
                                     @"condition" : @{
                                             @"53891" : @[@(123)],
                                             @"53892" : @[@(123),@(124)]}
                                     },

                             } success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                 success(operation,responseObject);
                             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                 failure(operation,error);
                             }];

But it's produce not expected output:

/api/sth.json?filter[condition][53891][]=123&filter[condition][53892][]=123&filter[condition][53892][]=124

There is a way to do this in parameters in AFHTTPRequestOperation or manually i have to put it into string?

EDIT:

My current solution is like that:

+(NSString*)convertFromDictionary:(NSDictionary*)dic {
    NSMutableString *outputStr = [NSMutableString new];
    [outputStr appendString:@"{"];
    NSArray *allKeys = [[dic allKeys] sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:nil ascending:NO]]];
    for(NSString *key in allKeys) {
        NSArray *objects = dic[key];
        [outputStr appendString:[NSString stringWithFormat:@"\"%@\":[",key]];
        for(NSNumber *nb in objects) {
            [outputStr appendString:[NSString stringWithFormat:@"%li",[nb longValue]]];
            if(![nb isEqual:[objects lastObject]]) {
                [outputStr appendString:@","];
            } else {
                [outputStr appendString:@"]"];
            }
        }
        if(![key isEqual:[allKeys lastObject]]) {
            [outputStr appendString:@","];
        }
    }
    [outputStr appendString:@"}"];
    return outputStr;
}

Where input dictionary is: @{@"53892" : @[@(123),@(124)]}

But it's nothing more than string compare. There is no more clever way to achieve it with AFNetworking directly since it's fairly standard URLs parameters?

like image 805
Jakub Avatar asked Jan 05 '16 13:01

Jakub


People also ask

Can URL contain curly braces?

Curly brackets are unsafe in URLs. cURL (unlike Google Chrome) tries to do you a favor and automatically encodes the URL. In other words, it transforms { to %7B and } to &7D . To prevent that behavior, you can pass the query string parameters using -d instead.

What are the curly brackets {{ }} used for?

In writing, curly brackets or braces are used to indicate that certain words and/or sentences should be looked at as a group. Here is an example: Hello, please pick your pizza toppings {chicken, tomatoes, bacon, sausage, onion, pepper, olives} and then follow me.

What are the curly braces in API?

Curly braces are used to define "dictionary literals," giving you the ability to declare a dictionary and its contents inside your program. A dictionary literal consists of a series of key/value pairs, written with a colon (:) between them, and with each of the key/value pairs separated from one other with commas.


2 Answers

You want 2 different ways of parsing a dictionary, and this cannot be done automatically. As Mehul said before, try serializing your parameter "condition" (converting its value to string) before creating the "parameters" dictionary:

NSError *error;
NSDictionary *dict = @{@"53891" : @[@(123)], @"53892" : @[@(123),@(124)]};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];

NSString *params = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

[self GET:myUrl parameters:@{
                                 @"filter" : @{
                                 @"condition" : params
                                 },

                         } success:^(AFHTTPRequestOperation *operation, id responseObject) {
                             success(operation,responseObject);
                         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             failure(operation,error);
                         }];
like image 118
nigelman Avatar answered Oct 08 '22 19:10

nigelman


You need to do this:

  1. create dictionary
  2. create json string from dictionary(first convert dictionary to NSData using NSJsonSerialization and then convert thata NSData object to NSString)
  3. Now append this string in your URL in format which you want to attach to
  4. create url from new string and pass it in get/post method with paramters dictionary as nil
like image 26
Mehul Thakkar Avatar answered Oct 08 '22 17:10

Mehul Thakkar