views:

376

answers:

3

I am having issues with my NSURLRequest and doing a POST whenever the string includes %, for example.

On my website, I can see that the encoding is ISO-8859-1,utf-8 for Accept-Charset but for some reason I cannot get the POST to work if it includes "%&$" etc. It seems like it won't encode it with UTF-8 encoding. Am I doing something wrong in the below code?

NSData *postData = [credentials dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.website.com/login.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
A: 

You may want to hear "Content-Transfer-Encoding".

Content-Type: text/plain; charset=UTF-8

Content-Transfer-Encoding: binary

or if you have problems specially with those chars

Content-Transfer-Encoding: base64

EDIT: The "application/x-www-form-urlencoded" makes the "%" signs happen.

Notinlist
+1  A: 

I'm not sure the exact answer to your problem, but try ASIHTTPRequest, specifically the ASIFormDataRequest and see if you get the same sort of problem. It's a very good library and encapsulates a lot of the encoding issues you might otherwise have to deal with yourself.

Best regards,

Matt Long
Thanks. Wondering if I just couldn't grab the `- (NSString*)encodeURL:(NSString *)string` method
Canada Dev
Actually. I think I'll implement the entire framework. Just read some of the code and it looks great.
Canada Dev
A: 

I had the EXACT SAME ISSUE, and its the URL encoding that's the issue (for example / = %2F). I found this snippet that will properly encode your text, as Apple's stringByAddingPercentEscapes is a severely broken method, and you shouldn't use it. Use this code below.

NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)credentials, NULL, (CFStringRef)@"!*'();:@+$,/?%#[]", kCFStringEncodingUTF8 );

Put this before the dataUsingEncoding method, and replace the dataUsingEncoding method with:

NSData *postData = [encodedString dataUsingEncoding:NSUTF8StringEncoding];
ckrames1234
Everything else is in that code block is good. And if you need further help, use Charles, an AWESOME web debugging proxy. It deconstructs your requests, so you can find whats wrong with them.
ckrames1234
Yeah, that code would work. However, I decided to go with the ASIHTTPRequest library as proposed by Matt above. It handles all that stuff for you, and has built-in methods for handling all kinds of character formats (inlcuding a method similar to yours), and implementing it only takes a few lines of code. Thanks!
Canada Dev
Yeah I looked at it and it is pretty sweet. I'm sticking to apple's way, though, because there are new SDKs, and the framework might get broken.
ckrames1234