1

I'm looking for how to do a POST request to a REST web service, specifically a CakePHP service and all I seem to find is people saying to use the ASIHTTPRequest library, but I see that's not been updated in a year or so, so I'd rather use a more up to date method, preferably directly from iOS.

I read you can make the call using NSURLConnection but I really don't understand it, I would like a more thorough explanation of it, if someone could point me in the right direction.

Also I have already successfully retrieved data from my server using my REST API by using NSString's stringWithContentOfURL method, but I can't use this to make a POST call as far as I know.

8vius
  • 5,786
  • 14
  • 74
  • 136

2 Answers2

5

I usually do it like this.

NSURL *url = [NSURL URLWithString:@"http://someurl.com"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:self.yourData];
NSHTTPURLResponse __autoreleasing *response;
NSError __autoreleasing *error;
[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
if (error == nil && response.statusCode == 200) {
    // Process response
} else {
    // Process error
}

[NSURLConnection sendSynchronousRequest:] method also returns NSData object which you too can manipulate.

Kyr Dunenkoff
  • 8,090
  • 3
  • 23
  • 21
  • How exactly is yourData formed, that I would say is my greatest doubt. Thank you. – 8vius Mar 01 '12 at 14:59
  • 1
    Yeah, it's a bit tricky. Simpliest way is to form `NSString` that looks like your average request string: `param1=value&param2=value&param3=value` - and then do `[yourString dataUsingEncoding:NSUTF8StringEncoding];`. Second way is to build `NSDictionary` with parameter-value pairs, serialize it into JSON data object (via `NSJSONSerialization` or `JSONKit`, I prefer latter) and send it within HTTP body, but to get in on server you'll have to read raw POST data, not $_POST. – Kyr Dunenkoff Mar 01 '12 at 15:04
  • Very helpful Kyr, have you ever used cake and see how they structure their data for form POST by any chance? – 8vius Mar 01 '12 at 15:08
  • Sorry, cannot help with CakePHP, didn't work it. But this code: `$postdata = file_get_contents("php://input"); $array = json_decode($postdata);` - will get you raw POST data and decode it into your standard PHP array. – Kyr Dunenkoff Mar 01 '12 at 15:12
  • We'll see if I have to resort to it, the thing with CakePHP is that it has a very specific naming convention, and I'd rather not tamper much with the methods I already have in the REST API. The names of the form fields in CakePHP have the following notation: name=data[Model][field] id=ModelField – 8vius Mar 01 '12 at 15:29
  • Also another thing, what about the encoding for this request? – 8vius Mar 01 '12 at 15:33
  • `data[Model][field]` is substituted for some string value, I recon. You'll just have to make sure that param names in your app look the same. As for encoding - it's UTF-8 by default, so probably should solve any issue with encodings. – Kyr Dunenkoff Mar 01 '12 at 15:34
  • EDIT: It's UTF-8 by default when converting strings, HTTPBody is `NSData`, so request is sent as raw data. – Kyr Dunenkoff Mar 01 '12 at 15:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8399/discussion-between-8vius-and-kyr-dunenkoff) – 8vius Mar 01 '12 at 15:42
  • Kyr, it's not working, I'm getting an error when I make the call – 8vius Mar 01 '12 at 18:24
  • dunenkoff@me.com, send me a msg. – Kyr Dunenkoff Mar 01 '12 at 19:19
1

I'm working on the same problem (iOS front end with a CakePHP REST POST backend). This thread has been very helpful. Try the following with curl:

curl -d "data[Model][field0]=field0value&data[Model][field1]=field1value" https://pathtoyourapi.com/plugin/controller/function.html

alternatively

curl -d "data[Model][field0]=field0value&data[Model][field1]=field1value" https://pathtoyourapi.com/plugin/controller/function
Pouria Almassi
  • 1,580
  • 2
  • 17
  • 26
  • Glad it helped, you should upvote my question *wink wink* :P but that would be in PHP I'm assuming, the curl call. The answer by Kyr solved all my issues, no problem making calls now to my API :) – 8vius Mar 27 '12 at 20:02
  • @8vius, I will when I have more reputation. I see the chat you and Kyr had but we're you able to finally get a response from posting your data to your cakephp resource? As I noted, I get the correct data response with curl but no luck yet with the method laid out by Kyr or with AFNetworking (switching over from ASIHTTPRequest). – Pouria Almassi Mar 28 '12 at 19:42
  • Post a question with your call code and what response you get, so I can give you a hand – 8vius Mar 28 '12 at 21:12