4

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

nurnachman
  • 4,468
  • 2
  • 37
  • 40
  • 1
    This has been discussed here: http://stackoverflow.com/questions/718429/creating-url-query-parameters-from-nsdictionary-objects-in-objectivec Hope this helps! – Goeran Feb 06 '12 at 12:02

1 Answers1

5

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);
Jon B
  • 51,025
  • 31
  • 133
  • 161
laxonline
  • 2,657
  • 1
  • 20
  • 37