1

Is there a simple way to escape a NSString so I can use it in a HTTP POST request?

I've tried stringByAddingPercentEscapesUsingEncoding, but the following:

[@"~!@#$%^&*()-+\"'" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 

gives me:

~!@%23$%25%5E&*()-+%22' 

which is not usable in a HTTP POST query because the & is left unescaped; therefore the other end interprets whatever follows it as another argument.

3

1 Answer 1

1

The following function, part of NSString+URLEncoding.m, does exactly what I need:

- (NSString *)encodedURLParameterString { NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(":/=,!$&'()*+;[]@#?"), kCFStringEncodingUTF8); return [result autorelease]; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.