O'Reilly logo

iOS 5 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.8. Sending HTTP PUT Requests with NSURLConnection

Problem

You want to call a web service using the HTTP PUT method to place a resource into the web server, and perhaps pass parameters as part of the HTTP body or in the query string, to the web service.

Solution

Just as with the GET, POST, and DELETE methods, we can use the PUT method using NSURLConnection. We must explicitly set our URL’s method to PUT.

Discussion

I have set up a dummy web service at the following address: http://pixolity.com/put.php. If you open this URL in your browser, you will see something similar to the screen shown in Figure 8-5:

The PUT web service opened in a web browser

Figure 8-5. The PUT web service opened in a web browser

This web service expects PUT requests and is able to print out parameters that are sent as part of the query string and as part of the HTTP body. So you can send both types of parameters in the same request. Let’s write a simple app that can create an asynchronous connection and send a few parameters as a query string, and a few parameters in the HTTP body to the aforementioned URL using the PUT method:

NSString *urlAsString = @"http://pixolity.com/put.php"; urlAsString = [urlAsString stringByAppendingString:@"?param1=First"]; urlAsString = [urlAsString stringByAppendingString:@"&param2=Second"]; NSURL *url = [NSURL URLWithString:urlAsString]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setTimeoutInterval:30.0f]; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required