0

This question is not specific to iOS but since I am developing the application in iOS, I see this issue.

I create the URL as

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *transactionsUrl = [NSString stringWithFormat:@"%@/%@/%@/%i/%i?categoryGroup=%@",
                                                       appUrl, @"rest/transactions", [userDefaults valueForKey:kMemberId], year, month, [categoryGroup stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"getting transactions for URL:%@", transactionsUrl);

My query parameter categoryGroup could be one of the following values

A & B, B & C,
what I see on `iOS client logs is

2014-12-31 21:38:44.654 myapp-ios[41275:70b] getting transactions for URL:https://myapp.com/rest/transactions/78f7f8a4-a8c9-454a-93a8-6633a1076781/2014/12?categoryGroup=A%20&%20B  

But on server, I see

categoryGroup=A

so the entire queryParameter is not received on the server.

What could be the potential issue?

daydreamer
  • 87,243
  • 191
  • 450
  • 722

2 Answers2

0

You need to encode the parameters:

http://en.wikipedia.org/wiki/Percent-encoding

The "&" becomes %26.

Andrew E
  • 7,697
  • 3
  • 42
  • 38
  • That's what he's trying to do with `stringByAddingPercentEscapesUsingEncoding`, the problem is that method just doesn't do what it's ostensibly supposed to. – Kevin Jan 01 '15 at 06:29
0

& separates GET parameters. Technically, on the server you're seeing categoryGroup = "A " ("A" followed by a space).

The underlying problem is that stringByAddingPercentEscapesUsingEncoding doesn't escape things that it should (and does escape things it shouldn't), like &. Use CFURLCreateStringByAddingPercentEscapes instead, per this answer

NSString *encodedString = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8));
Community
  • 1
  • 1
Kevin
  • 53,822
  • 15
  • 101
  • 132