Objective-C in the Cloud

Response Types

Responses are created when a request comes in. The response is delivered to the client. This process is started as soon the response is passed to the request by calling the method -respondWith:. The only parameter of the respondWith-method is typed with id. This allows you to pass different kinds of response objects: In some situations a simple string is a good response and in some other situations you might need something more sophisticated. These are the different kind of response objects you can return:

  • A simple string: This will create a HTTP response with a status code of 201, content type will be plain/text and the body will be the unicode representation of the string you returned.
  • A dictionary: This allows you to specify a custom status code, body and custom headers. The returned dictionary should look something like this:
1 @{@"status" : @201, 
2   @"headers": @{"Content-Type" : @"image/tiff" }, 
3   @"body" : [image TIFFRepresentation]}
  • A OCFResponse object: OCFResponse is a class which represents a response. You can create and pass a OCFResponse which gives you full control.
  • A OCFMustache object: OCFMustache is a class that represents a Mustache template response. At the moment Mustache is the only template engine supported by OCFWeb. You can have template files in your application bundle and then create a OCFMustache object by using +newMustacheWithName:object: which renders the template so that it is ready to be delivered to the client.

Example: String Response

The following example shows how to create a response by returning a simple string.

1 self.app = [OCFWebApplication new];    
2 self.app[@"GET"][@"/"] = ^(OCFRequest *request) {
3   [request respondWith:@"Hello World. I am a string."];
4 };
5 [self.application runOnPort:8080];

This creates a plain/text response with a status code of 201. If you don't like the content type or status code for string responses you can change it on a per application basis.

Example: Dictionary Response

The following example shows how to create a response by returning a dictionary. If you run this example you should be able to use your browser to access the web application which should display your application icon.

1 self.app = [OCFWebApplication new];    
2 self.app[@"GET"][@"/"] = ^(OCFRequest *request) {
3   NSImage *image = [NSImage imageNamed:@"NSApplicationIcon"];
4   [request respondWith:@{ @"status" : @201,
5                          @"body" : [image TIFFRepresentation],
6                          @"headers" : @{ @"Content-Type" : @"image/tiff" }}];
7 };
8 [self.application runOnPort:8080];

Example: Mustache Response

For the following example to work there must be a file called Detail.mustache in the resources of your application. Before using a mustache response you should read the documentation of the mustache library used by OCFWeb. A mustache file basically contains text with placeholders and the underlying mustache engine can automatically fill in the details for you.

 1 self.app = [OCFWebApplication new];
 2 self.app[@"GET"][@"/"]  = ^(OCFRequest *request) {
 3   NSDictionary *person = @{ @"id" : @1,
 4                             @"firstName" : @"Christian",
 5                             @"lastName" : @"Kienle" };
 6   OCFMustache *response = [OCFMustache newMustacheWithName:@"Detail"
 7                                                     object:person];
 8   [request respondWith:response];
 9 };
10 [self.app runOnPort:8080];