Mobile Developer
Mobile Developer

Reputation: 5760

proper NSURL request encoding

I need to encode the body of my POST request but I don´t know which one to use. I´m having a trouble with changing ´(´ into %28. I was using so far NSUTF8StringEncoding, NSISOLatin1StringEncoding, NSISOLatin2StringEncoding. Non of them works.

The name of the parameter I have to send is:

monitoring_report[monitoring_report_time(1i)]=

which sould be transformed to:

monitoring_report%5monitoring_report_time%281i%29%5D=

and what I get is:

monitoring_report%5Bmonitoring_report_time(1i)%5D=

Upvotes: 0

Views: 423

Answers (1)

omz
omz

Reputation: 53551

Use the Core Foundation function CFURLCreateStringByAddingPercentEscapes which has a legalURLCharactersToBeEscaped parameter. Parenthesis in URLs are legal which is why they're not escaped by default.

Example:

NSString *input = @"monitoring_report[monitoring_report_time(1i)]=";
NSString *output = [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)input, NULL, CFSTR("()"), kCFStringEncodingUTF8) autorelease];
NSLog(@"%@", output); // monitoring_report%5Bmonitoring_report_time%281i%29%5D=

Upvotes: 2

Related Questions