Question

I am calling the following URL from my iPhone app

NSString *resourcePath = [NSString stringWithFormat:@"/sm/search?limit=100&term=%@&types[]=users&types[]=questions", searchTerm];

However, when the server call is made, I found that the actual URL sent was this

/sm/search?term=mi&types%5B%5D=questions&limit=100

How can I resolve this so that the correct URL is sent to the server?

Was it helpful?

Solution

I assume that you use the NSURL method

initWithScheme:host:path:

to create the URL. According to the documentation, this method automatically escapes path with the stringByAddingPercentEscapesUsingEncoding: method.

The square brackets are escaped, because they are "unsafe" in the sense of RFC 1738 - Uniform Resource Locators (URL):

Characters can be unsafe for a number of reasons. [...] Other characters are unsafe because gateways and other transport agents are known to sometimes modify such characters. These characters are "{", "}", "|", "\", "^", "~", "[", "]", and "`".

All unsafe characters must always be encoded within a URL.

If you create the URL with

NSString *s = [NSString stringWithFormat:@"http://server.domain.com/sm/search?limit=100&term=%@&types[]=users&types[]=questions", searchTerm];
NSURL *url = [[NSURL alloc] initWithString:s];

then no escape sequences are added, because initWithString expects the string to contain any necessary percent escape codes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top