WSContext

Defines an injection variable to retrieve REST operation context values at the service level.

Syntax

WSContext

WSContext is an optional attribute.

Usage

You can use it to retrieve REST operation context such as BaseURL, Media, and Scope, or you can set a default context for the Content-Type header if the WSMedia attribute value contains a wildcard.

You need to define a DICTIONARY variable in your REST module that specifies WSContext in an ATTRIBUTE()clause.

Example definition for WSContext dictionary

Important: The context variable needs to be a modular variable.

PRIVATE DEFINE Context DICTIONARY ATTRIBUTE(WSContext)OF STRING
The GWS engine sets the dictionary key/values before the function is executed. For examples of use in your REST function, see Table 1.
Table 1. Context dictionary
Key Description Example usage
Media Provides one of the media values set in the WSMedia attribute of the input parameter. Or it provides the defaults if there is no attribute, or what the REST engine has chosen when several are possible.
DISPLAY context["Media"] 
This may display, for instance, application/xml. If, for instance, WSMedia is set with a list of mime types, ("application/json,application/xml"), you can know the exact media type requested in the current execution of the function.
BaseURL Provides the base URL of the service when executed. For example, the URL is not the same in standalone as behind a GAS.
DISPLAY context["BaseURL"]
This displays the service's BaseURL, such as: HTTPS://host:port/gas/ws/r/xcf/Account if the service has been registered as "Account".

This may be useful if your REST function has to query another function, for instance, and when the exact URL is needed.

Scope Provides the valid scope (if there is one) that grants access to the REST function.
DISPLAY context["Scope"]
This may display "profile.read".

If, for instance, WSScope (function) has been set in the function with a list of scopes (for example, "profile.read, profile.write, profile.mgr"), it may be useful to know which one really applies in the current function execution.

Content-Type Provides the real MIME-type the REST function returns if the WSMedia attribute value contains "image/*".
Important: This is the only context dictionary value you can set at runtime.
LET context["Content-Type"]="image/jpeg"
For example, this statement will set the returned Content-Type header to "image/jpeg" based on the media type requested at runtime.

Set WSContext with Content-Type at runtime

If the WSMedia return parameter attribute has a list of values or a wildcard value, "image/*", you code in a function to set the Content-Type header for the actual value returned at runtime in the response. Table 2 outlines options for defining the Content-Type header for the image.
Table 2. HTTP Header
Header Name Description Required Values
Content-Type The format of the image to upload. Optional image/jpeg, or image/png, or image/gif, and so on.

Example WSContext with Content-Type set at runtime

PRIVATE DEFINE Context DICTIONARY ATTRIBUTE(WSContext) OF STRING

PUBLIC FUNCTION getImage( id INTEGER ATTRIBUTE(WSParam) )
  ATTRIBUTES(WSGET,
             WSPath="/photos/{id}" )
  RETURNS STRING ATTRIBUTE (WSAttachment, WSMedia="image*/") 
    DEFINE image BYTE
    DEFINE mime STRING
    # function code
    CASE mime
    WHEN "png"  
      LET Context["Content-Type"]="image/png"
    WHEN "jpg"
      LET Context["Content-Type"]="image/jpg"
    END CASE
    RETURN image
END FUNCTION