Configuring callback functions

You configure high-level REST preprocessing and postprocessing of requests via attributes set on functions in your REST web service module.

Preprocessing performs setup operations before a REST operation executes a request and postprocessing performs transformation operations after the request is executed and before the REST response is forwarded to the client. To implement preprocessing and postprocessing, you must add predefined callback functions to your REST web service module. For sample callback functions, go to Examples of callback functions.

The REST engine will manage these callbacks for all operations of your web service. A callback function typically modifies query, header, body, and input and return parameter values of the REST request. For example:
  • preprocessing can consist of modifying values in the input expected by the REST operation; a database field may have changed and the query needs to be adjusted.
  • postprocessing may involve transforming data depending on the content type; an image may need to be resized or data format need to be transformed before being returned to the client.

If a REST operation has no requirements of a callback function, the REST engine returns control back to the operation to continue standard REST service processing.

To enable preprocessing and postprocessing functionality, you must:
  • Add predefined callback functions in your REST web service module. For sample callback functions, go to Examples of callback functions.
  • Define these functions via two attributes, WSPreProcessing and WSPostProcessing.
  • Define these functions as private functions to distinguish them from the public functions of your web service.
The REST engine fills the WSContext dictionary with the resource path, the HTTP verb, and the function name of the operation currently processing a request so that the callback returns into the context from where it was called.

Predefined order of executing callbacks

Typically, when a request is made to a web service operation, the REST engine will execute the callbacks (if defined) in a predefined order. Callbacks are called in the following sequence from 1 to 7; preprocessing functions are called before the REST engine starts to process the incoming request. After the REST engine executes the request (step 4), then the postprocessing functions are called before returning the response. You may use only one or all depending on your needs:
  1. HTTP incoming callback

    • Depending on the return code, the request may return immediately to the client or continue to process the request.

  2. REST parameter, query, header, and cookie incoming callback (if any)

    • Allows you to modify a REST parameter before calling a REST operation.

  3. REST body incoming callback (depending on the request body content-type)

    • Allows you to modify the body before calling the REST operation.

  4. Execute the standard REST operation

  5. HTTP outgoing callback.

    • Depending on the return code, the response HTTP return code can be changed.

  6. REST header outgoing callback

    • Allows you to modify REST header parameter before returning the response.

  7. REST body outgoing callback (depending on the response body content-type)

    • Allows you to modify the HTTP returned body.

For sample callback functions, go to Examples of callback functions.