I have a simple POST coming from my iphone app. Its working fine, except passing an ampersand causes the backend to break – it’s almost like its treating it like a GET request (ampersands seperate the variable names). Do I need to do some kind of encoding first? Here is the code:
NSString *content = [[NSString alloc] initWithFormat:@"data=%@&email=%@", str, emailAddress.text];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.myurl.com/myscript.php"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[content dataUsingEncoding:NSISOLatin1StringEncoding]];
// generates an autoreleased NSURLConnection
[NSURLConnection connectionWithRequest:request delegate:self];
Edit: As
stringByAddingPercentEscapesUsingEncoding:should be used to encode parts of the query, not the whole one, you should be using another method instead.Unfortunately, Foundation doesn’t provide such a method, so you need to reach to CoreFoundation:
You can use
In your case it will look like this:
You can do this in this way, too: