1

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?

aur0n
  • 463
  • 3
  • 14
  • 25
  • Use ASIFormDataRequest for simplicity. http://allseeing-i.com/ASIHTTPRequest/How-to-use – Luke Mar 01 '12 at 15:37
  • Well, you can also look at their class files and see how they implement it - you may find your answer specifically in there, else it will be good for you to compare implementations. – Luke Mar 01 '12 at 15:40
  • ASIHTTPRequest is discontinued, look [here](http://allseeing-i.com/%5Brequest_release%5D) for alternatives. – lawicko Mar 01 '12 at 15:42
  • I see traces of GET and POST techniques in your example, so I'm not clear which you actually want to do. How do you want to get this data up? If it's really POSTing, do you want to use URL encoding or multipart? – Steven Fisher Mar 01 '12 at 15:57

2 Answers2

4

You will have to:

Dirk
  • 30,623
  • 8
  • 82
  • 102
1

It looks like your parameters are intended to be URL parameters (typically in name1=value1&name2=value2 form). You usually don't want to put them in the HTTP body like you are currently doing. Instead, append them to your URL:

NSString *requestStr = @"hello";
NSString urlString = [NSString stringWithFormat:@"http://mydomain.com/page.php?str=%@", requestStr];
NSURL *url = [NSURL URLWithString:urlString];

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.

Community
  • 1
  • 1
Tim Dean
  • 8,253
  • 2
  • 32
  • 59
  • Thanks, but in this way the parameter(s) are passed with the URL, and for security reasons I would like to avoid that. – aur0n Mar 01 '12 at 15:56
  • If your service is responding that the str value is not set, then it is likely that it is expecting the parameters in the URL whether you want it to be there or not. If you want these values to be in the body that's fine, but your service needs to expect them there. In that case the client and service side will need to agree on what format the body should be. You could of course use any number of name=value pairs in the body, but the more common convention is to use a JSON or XML format when posting data in the HTTP body. – Tim Dean Mar 01 '12 at 15:59
  • And if your service is really expecting the parameters in the body in this format, rather than using JSON or XML, see the answer from @Dirk on how to specify that your HTTP body is using form URL encoded content – Tim Dean Mar 01 '12 at 16:07