Re: Parse form values from HTTP POST
Re: Parse form values from HTTP POST
- Subject: Re: Parse form values from HTTP POST
- From: Jesse Grosjean <email@hidden>
- Date: Thu, 21 Aug 2008 09:15:49 -0400
So now at least I have a more specific question. Is there any OS X
framework for decoding x-www-form-urlencoded, or sample code
floating around. I've found what I need in Java:
http://kickjava.com/src/java/net/URLDecoder.java.htm
And while it's not that much code, it seems like the kind of thing
that must have been done before. Anyway I'll start on my own
implementation, but if someone can point me to an existing solution
for decoding x-www-form-urlencoded that would make my day :)
isn't that java code is just
[urlString
stringByReplacingPercentEscapesUsingEncoding:encodingFromHTTPHeader
]; ?
You are right... Thanks! I'd tried
stringByReplacingPercentEscapesUsingEncoding first, but I think I
might have been messing up the encoding somewhere else the process.
And that lead me to believe that
stringByReplacingPercentEscapesUsingEncoding wasn't going to work for
what I needed. But after your message I went back to it, and got
things to work. For future reference here's what I'm doing that seems
to work:
- (NSData *)requestBody {
return [(id) CFHTTPMessageCopyBody(request) autorelease];
}
- (NSString *)requestBodyString {
return [[[NSString alloc] initWithData:self.requestBody
encoding:NSUTF8StringEncoding] autorelease];
}
- (NSDictionary *)requestBodyWWWFormURLDecoded {
NSMutableDictionary *results = [NSMutableDictionary dictionary];
for (NSString *eachPair in [[self.requestBodyString
stringByReplacingOccurrencesOfString:@"+" withString:@" "]
componentsSeparatedByString:@"&"]) {
NSArray *eachPairArray = [eachPair componentsSeparatedByString:@"="];
if ([eachPairArray count] == 2) {
[results setObject:[[eachPairArray objectAtIndex:1]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
forKey:[eachPairArray objectAtIndex:0]];
} else {
NSLog(@"Pair array without pair: %@", eachPairArray);
}
}
return results;
}
And for more future reference this code isn't really complete, it's
assuming UTF8, and doesn't handle multipart posts, but it works for my
needs.
Thanks again.
Jesse
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden