Objective-C in the Cloud

OCFRequest Class Reference

Declared inOCFoundation/OCFResponse.h
Base classNSObject
GuidesRequest based Applications
Request based Applications in Detail

Overview

An instance of OCFRequest represents an HTTP request. Instance objects of OCFRequest and not created by your cloud application, but provided by the Objective-Cloud runtime environment when a request for your application is received. To a request-based service the object is passed as an argument of the handler. Inside an invocation-based service the request object is a property of the current invocation object that can be retrieved with +currentInvocation.

You usually use the provided instance object to introspect the the content of the request and send a response to it. Running a request-based service you have to do this manually. For invocation-based services this is done by the runtime environment automatically for arguments and return values. But there are still reasons to deal with instance objects of OCFRequest manually, especially if you want to read additional information from the request or want to respond in a non-JSON format (i. e. HTML.) However, using the request object in an invocation-based service extensively is a code smell.

You can respond to a request with an instance object of OCFResponse or for convenience with instance objects of NSString, NSDictionary, or OCFMustache respectively as described for -respondWith:.

Tasks

Responses

- respondWith:

Properties

@method
@URL
@headers
@path
@query
@contentType
@contentLength
@data
@host
@cookies
@parameters @userBadges

Declared Properties

contentLength

The content length.

@property (nonatomic, assign, readonly) NSUInteger contentLength

This property holds the content length of the request.

contentType

The content type.

@property (nonatomic, copy, readonly) NSString *contentType

This property holds the content type of the request. This is a computed property for convenience which should be equivalent to headers[@"Content-Type"].

cookies

The cookies.

@property (copy, readonly) NSArray *cookies

Discussion

The cookies contained in the request contains as instances of NSHTTPCookie..

data

The body.

@property (nonatomic, copy, readwrite) NSData *data

Discussion

The HTTP body of the request as an instance object of NSData.

headers

The header fields.

@property (nonatomic, copy, readonly) NSDictionary *headers

Discussion

The header fields of the request.

host

The host part of the URL.

@property (readonly) NSString *host

Discussion

The host part of the request's URL#p_URL) that means of the host that has been requested.

The string does not contain the port. Objective-Cloud uses the default port for HTTPS (443) and locally the Port 10000 for HTTP.

method

The HTTP method.

@property (nonatomic, copy, readonly) NSString *method

Discussion

This property holds the HTTP method used in the request, i. e. GET, POST, PUT, or DELETE)

        <td><code>-path</code></td>
        <td>NSString</td>
        <td>Returns the path of the requested resource. </td>

method

The HTTP method.

@property (nonatomic, copy, readonly) NSString *method

Discussion

This property holds the HTTP method used in the request, i. e. GET, POST, PUT, or DELETE)

parameters

The path of the request.

@property (nonatomic, copy, readonly) NSDictionary *parameters

Discussion

A property holding the parameters of the HTTP request.

Parameters can be many things: For example if your route contains a placeholders like /path/:id/:action then the parameters dictionary contain the keys and values for the placeholders.

path

The path of the request.

@property (nonatomic, copy, readonly) NSString *path

Discussion

The path used in the request.

query

The query of the URL.

@property (nonatomic, copy, readonly) NSDictionary *query

Discussion

Returns the query of the URL as a dictionary according to RFC 1801.

URL

The URL.

@property (nonatomic, copy, readonly) NSURL *URL

Discussion

The URL used in the request.

userBadges

The content length.

@property (nonatomic, strong, readonly) NSMutableDictionary userBadges

You can use the dictionary to store your own information related to the request. Keys starting with com.objective-cloud are reserved for internal use.

Instance Methods

respondWith:

Sends a respond to the request.

- (void)respondWith:(id)response

Parameters

response
The response value.

Discussion

This method is used to pass a response back to the client. After responding the request is closed and you cannot send additional data (one-shot).

The response argument can be an instance object of the following classes:

  • OCFResponse: A fully configurable response object.
  • NSString: A string used as the body of the response. The header is automatically set.
  • NSDictionary: A dictionary with the response data as described below.
  • OCFMustache: A mustache instance object.

If you choose a dictionary response you can set three key-value pairs:

  • status: An NSNumber object containing the HTTP response status.
  • headers: A dictionary with key-value pairs for the header fields.
  • body: An instance object of NSData containing the response body.

A typical dictionary response looks like this:

// Responding with the application icon
NSImage *image = [NSImage imageNamed:@"NSApplicationIcon"];
[request respondWith:@{ @"status" : @201,
                        @"body" : [image TIFFRepresentation],
                        @"headers" : @{ @"Content-Type" : @"image/tiff" }}];
};