It wasn’t too difficult to build a REST client in Cocoa for my iPhone app, but there was enough to it that I wanted to wrap it in a service, so I could make a REST call in one line. And so I could reuse it and pass it along to you!
Download a zip file containing the header and class file, or see the code below.
Incidentally, if you’re wondering how I create/consume the data, I use json-framework. It’s very easy to integrate into your project, and converts JSON objects to/from NSMutableDictionaries / NSMutableArrays.
Usage
Note: I haven’t tested this exact usage code, so you may need to tweak it.
RestService *svc = [[RestService alloc] init];
NSHTTPURLResponse *response = nil;
// simplest request: GET, no headers
NSString *result = [svc requestToUrl:@"http://..."
response:&response];
NSLog(@"status code: %d", response.statusCode);
NSLog(@"content: %@", result);
// most complex request: all parameters
NSDictionary headers = [NSDictionary dictionaryWithObjectsAndKeys:
@"application/json", @"Content-Type"
@"Basic ...", @"Authorization",
nil];
NSString *requestBody = @"some xml or json";
NSString *result = [svc requestToUrl:@"http://..."
method:METHOD_POST
body:requestBody
headers:headers
response:&response];
NSLog(@"status code: %d", response.statusCode);
NSLog(@"content: %@", result);
RestService.h
//
// RestService.h
// TypeLinkNav
//
// Created by Josh Justice on 10/21/10.
// Copyright 2010 Josh Justice. All rights reserved.
//
#import
#define METHOD_GET @"GET"
#define METHOD_PUT @"PUT"
#define METHOD_POST @"POST"
#define METHOD_DELETE @"DELETE"
@interface RestService : NSObject {
}
-(NSString *)requestToURL:(NSString *)url
response:(NSHTTPURLResponse **)response;
-(NSString *)requestToURL:(NSString *)url
headers:(NSDictionary *)headers
response:(NSHTTPURLResponse **)response;
-(NSString *)requestToURL:(NSString *)url
method:(NSString *)method
headers:(NSDictionary *)headers
response:(NSHTTPURLResponse **)response;
-(NSString *)requestToURL:(NSString *)url
method:(NSString *)method
body:(NSString *)body
headers:(NSDictionary *)headers
response:(NSHTTPURLResponse **)response;
@end
RestService.m
//
// RestService.m
// TypeLinkNav
//
// Created by Josh Justice on 10/21/10.
// Copyright 2010 Josh Justice. All rights reserved.
//
#import "RestService.h"
@implementation RestService
-(NSString *)requestToURL:(NSString *)url
response:(NSHTTPURLResponse **)response
{
return [self requestToURL:url
headers:nil
response:response];
}
-(NSString *)requestToURL:(NSString *)url
headers:(NSDictionary *)headers
response:(NSHTTPURLResponse **)response
{
return [self requestToURL:url
method:METHOD_GET
headers:headers
response:response];
}
-(NSString *)requestToURL:(NSString *)url
method:(NSString *)method
headers:(NSDictionary *)headers
response:(NSHTTPURLResponse **)response
{
return [self requestToURL:url
method:method
body:nil
headers:headers
response:response];
}
-(NSString *)requestToURL:(NSString *)urlString
method:(NSString *)method
body:(NSString *)body
headers:(NSDictionary *)headers
response:(NSHTTPURLResponse **)response
{
// create the url
NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"requesting %@, %@",method,url);
NSLog(@"request body: %@", body);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// set up the request
if( nil != method ) {
[request setHTTPMethod:method];
}
if( nil != body ) {
NSData *bodyData = [body dataUsingEncoding:NSASCIIStringEncoding];
[request setHTTPBody:bodyData];
}
if( nil != headers ) {
NSEnumerator *e = [headers keyEnumerator];
NSString *headerName;
while( headerName = [e nextObject] ) {
[request setValue:[headers objectForKey:headerName]
forHTTPHeaderField:headerName];
}
}
// get the response
NSHTTPURLResponse *myResponse = NULL;
NSError *error = NULL;
NSData *resultData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&myResponse
error:&error];
// pass response object back to user by reference
*response = myResponse;
// convert response body to string
NSString *resultString = [[NSString alloc] initWithData:resultData
encoding:NSASCIIStringEncoding];
NSLog(@"response body: %@", resultString);
return resultString;
}
@end
Hi!
Really like your REST client, very easy to use! However, have problems when I connect through a proxy. Do you have any tips on how I can solve it?