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?
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.
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.
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.
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);
}];
You need to do this:
NSData
using NSJsonSerialization
and then convert thata NSData object to NSString)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With