Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Tamara J
Tamara J
189 Points

Sending NSString to Server using the HTTP Post Method - Need Help!


I'm new to Objective-C (programming in general) and wish to sending a NSString to my server and back to another user's phone. I'm having trouble implementing POST as most tutorials on this subject are not clear and was hoping that someone can clear it up for me. Any documentation on this subject? I'll even be okay with documentation using SDK like Parse's or any other type of SDK. Thank you for any help.

2 Answers

In a nutshell, you have to use NSMutableURLRequest and NSURLSession (The NSURLSession sends the NSMutableURLRequest). First, create a new NSMutableURLRequest:

NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"YOUR URL HERE"]];
[req setHTTPMethod:@"POST"];

Now, we need to attach the POST data (your NSString) to the NSMutableURLRequest. To do that, take your string (let's say it's called str) and convert it to NSData:

NSData *postData = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

Then, we need to set the Content-Length header in the POST request (so your server knows how much content it's getting) and attach our post data to the request:

NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
[req addValue:postLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPBody:postData];

Now we're done configuring our NSMutableURLRequest! Next, we have to create an NSURLSession:

NSURLSession *session = [NSURLSession sharedSession];

Then, you should use the [-dataTaskWithRequest:completionHandler] method on NSURLSession to send the request and pass in a callback that you want to have executed when the response comes back. The way it works is you get an NSURLSessionDataTask back, and you just call [-resume] on it:

NSURLSessionDataTask *task = [session dataTaskWithRequest:req
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                // Do something with response data here - convert to JSON, check if error exists, etc....

[task resume];

Now your request is sent, and your completionHandler will be called whenever you get a response!

Tamara J
Tamara J
189 Points

Hey, thank you so much! I took a long break but now that I'm back I'll try this.