Searching the web for some method to send POST requests in Objective-C, I came up with some solutions.
That’s what I’ve got:
responseData = [NSMutableData new];
NSURL *url = [NSURL URLWithString:@"http://mydomain.com/page.php?"];
NSString *myParameters = [[NSString alloc] initWithFormat:@"str=hello"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[myParameters dataUsingEncoding:NSUTF8StringEncoding]];
The response I receive is an error, because the POST variable “str” hasn’t been set.
What am I doing wrong?
It looks like your parameters are intended to be URL parameters (typically in
name1=value1&name2=value2form). You usually don’t want to put them in the HTTP body like you are currently doing. Instead, append them to your URL:NSURLRequest doesn’t provide a more generic way to do this, although you can look at this SO question and its answers for ideas on how many people deal with this kind of requirement. There are also free and/or open source libraries out there that aim to make this kind of request easier to code.