I just switched from ASIHTTPRequest to AFNetworking.
My app allows a user to enter a search term, and then I make an api call to my server (rails) to retrieve a list of relevant objects to display on the phone. Simple enough.
Too Long, Won’t Read
With the ASI library, the search term sent to the server would be something like st%20helena. Now, with the AFNetworking, my search term sent to the server is st%2520helena. Oddly enough, this is actually making a difference. With %20, I receive no results. With %2520, I receive the results I was expecting.
My question is, why does this make a difference? I know that %2520 is an encoded ‘%’ + 20, which equals a whitespace character, which in my mind should be identical to passing %20.
Details
Before, I was simply appending the search term to the URL after first encoding it:
NSString *encoded = [@"st helena" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *url = [NSString stringWithFormat:@"http://www.example.com/?=%@, encoded];
Now, with AFNetworking, I’m passing an NSDictionary to the parameters argument of AFHTTPClient#getPath.
NSString *encoded = [@"st helena" stringByAddingPercentEscapesUsingEncoding:NSUTR8StringEncoding];
NSDictionary *dict = @{ @"q" : encoded };
[[myClient sharedClient] getPath:@"" parameters:dict ...]
Since AFNetworking encodes items in the dictionary, my search string is being double encoded, which is why the % of ‘%20’ is being converted into ‘%25’.
No, they aren’t identical. The parsing code isn’t supposed to just keep decoding over and over again until there aren’t any more percent signs left. If that were the case, it would be impossible to transmit the actual percent character. It’s only supposed to decode it once. This means that if
%2520goes over the wire, it is decoded into the data%20and processing stops there. If%20goes over the wire, it is decoded once into a space. The decoding is only supposed to happen once. If your web service is treating%2520as a space, then it has a double-decoding bug.