Welcome to Day 8 in the 31 Days of iOS.  In yesterday’s article, we discussed how to perform network requests using the NSURLRequest and NSURLConnection classes.  We showed two different methods of making these requests: one of which required us to implement several delegate methods and the other made use of a new technique called a block.  Today we’re going to continue where we left off and talk about how to change from doing a GET request to doing a POST request.  We’ll also look at how to change the content type and post JSON data.  You can start out with the code we finished up with yesterday or, if you’d like to follow along with the completed code, you can access it here.

Changing the HTTP Method

In order to make any modifications to the request we’re sending to the server (including changing the HTTP method from GET to POST, posting data, or changing the content type of the data being posted, we need to change the type of request we’re using.  Previously we were initializing a NSURLRequest, but like other NS* objects that aren’t NSMutable* objects, these objects aren’t mutable and therefore can’t be changed.  To fix this, we can convert our NSURLRequest to a NSMutableURLRequest.  This will allow us to change properties after the request has been initialized.  The first method we’re going to use on our NSMutableURLRequest is setHTTPMethod.  This allows us to change the HTTP method:

When we run the app now, nothing will be different from yesterday.  This is because the web page we’re posting to is capable of handling both GET and POST requests.  Frequently you’ll find that  when you need to post data to a web service, a GET request will not work at all. 

Posting data

In order to post data, we’ll use another method on the NSMutableURLRequest object: setHTTPBody.  However, before we can call that method, we need to get the post values into a format we can use.  Specifically we need either NSData or NSMutableData.  We can create one of those objects by taking a NSString and calling it’s dataUsingEncoding method:

Here we’re putting the value we want to post into a string where it’s being set to a post value named postedValue.  We put that into a NSData object and then finally call setHTTPBody.  Now when we run our app, we’ll see that the posted value is now showing up:

Day 8 Posted Values

While that works, what we really want to do is figure out how to post JSON or XML.

Changing the content type and posting JSON

Posting either JSON or XML data ends up being very simple.  We first change the HTTP method to POST as we did above.  We then call another method which allows us to set the Content-Type header on the HTTP request.  Sending JSON data is something that could be complicated and require us to pull down a third party library.  Thankfully, with the release of iOS 5, Apple introduced the NSJSONSerialization class to help us with JSON.  Let’s look at that now:

First we need to generate a NSDictionary with the key-value pairs that will go in our JSON.  We then use NSJSONSerialization to convert the NSDictionary into NSData.  That object is then used to set the HTTP body of our request.  Notice the use of the NSJSONWritingPrettyPrinted option.  Using that causes the JSON that is generated to be “pretty” or contain whitespace.  This is useful for debugging, however, it isn’t the most efficient way to send data over the wire.  To get the most minimal JSON possible, this flag should be removed so unneeded whitespace isn’t generated. 

Conclusion

Today we continued to look at how to perform network requests from iOS apps.  Now we know how to do different HTTP requests as well as send data over the wire (in addition to as part of the query string).  We also looked at how to send JSON data over the wire as that is a very common practice when communicating from mobile clients to web servers.  The knowledge from these two posts should prepare you for doing just about any network communication.  You can access the finished source code from this article here.


Chris Risner


Leave a Comment