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?

4
  • Use ASIFormDataRequest for simplicity. allseeing-i.com/ASIHTTPRequest/How-to-use Commented Mar 1, 2012 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. Commented Mar 1, 2012 at 15:40
  • ASIHTTPRequest is discontinued, look here for alternatives. Commented Mar 1, 2012 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? Commented Mar 1, 2012 at 15:57

2 Answers 2

4

You will have to:

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

Thanks, but in this way the parameter(s) are passed with the URL, and for security reasons I would like to avoid that.
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.
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.