开发者

Ampersand in POST request causing havoc

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] i开发者_StackOverflow中文版nitWithURL:[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];


I had this issue in iOS7 and the accepted answer didn't work at all (actually, that is my standard when sending data to the backend). The ampersand was breaking in the backend side, so I had to replace the & by %26. The backend was being done in python and the code was legacy and was using ASI.

Essentially I have done the following:

NSString *dataContent =  [NSString stringWithFormat:@"text=%@", 
    [json stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

dataContent = [dataContent stringByReplacingOccurrencesOfString:@"&" 
                                                     withString:@"%26"];


ByAddingPercent....... will not work as & is a valid URL character.

I needed to send a JSON with & in it, it is the same idea though;

NSString *post = [NSString stringWithFormat:@"JSON=%@", (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)jsonString, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8))];

"jsonString" towards the end is what is converted.


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:

- (NSString *)stringByURLEncodingString:(NSString *)string {
    return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
        kCFAllocatorDefault,
        (__bridge CFStringRef)string,
        NULL, // or (__bridge CFStringRef)(@"[].")
        (__bridge CFStringRef)(@":/?&=;+!@#$()',*"),
        kCFStringEncodingUTF8
    );
}

You can use

- stringByAddingPercentEscapesUsingEncoding:

In your case it will look like this:

NSString * content = [[NSString alloc] initWithFormat:@"data=%@&email=%@", [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [emailAddress.text  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

You can do this in this way, too:

NSString *dataStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *emailStr = [emailAddress.text  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *content = [[NSString alloc] initWithFormat:@"data=%@&email=%@", dataStr, emailStr];


I'm not sure if this will work in your case, but you could try %38 to try and encode the ampersand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜