nurnachman
nurnachman

Reputation: 4565

How to add GET parameters to an ASIHttpRequest?

How can I add GET parameters to an ASIHttpRequest?

I want to go from http://mysite.com/server to http://mysite.com/server?a=1&b=2 programmatically.

I have all my parameters as key-value pairs in an NSDictionary object.

Thanks

Upvotes: 4

Views: 2505

Answers (1)

laxonline
laxonline

Reputation: 2653

Use string format like

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"1",@"a",@"2",@"b", nil];

NSMutableString *prams = [[NSMutableString alloc] init];

for (id keys in dict) {
    [prams appendFormat:@"%@=%@&",keys,[dict objectForKey:keys]];
}
NSString *removeLastChar = [prams substringWithRange:NSMakeRange(0, [prams length]-1)];
NSString *urlString = [NSString stringWithFormat:@"http://mysite.com/server?%@",removeLastChar];

NSLog(@"urlString %@",urlString);

Upvotes: 5

Related Questions