The wwRestService
class is a specialized wwProcess
class that can be used for REST services. This class creates REST services using FoxPro method as HTTP endpoints, with methods receiving a single input parameter that is parsed from a JSON payload, and returning a FoxPro result - a value, object, collection or cursor - which is then turned into a JSON HTTP response.
The class also provides for automatic error handling, error response results and Bearer Token Authentication.
A Rest process has these features:
Create a Method with simple parameter input and output
REST methods are implemented receive parameters that are parsed from JSON, and return values as plain FoxPro values, objects, collections, arrays or cursors. Typically you don't use Request and Response objects much, but rely on API parameters to pass in data and simply return values/objects.JSON parsing and JSON and XML Output
For each request a single value/object can be passed as a JSON object from the client and get mapped to the endpoint input parameter. Any FoxPro return values from the endpoint method are by default turned to JSON and optionally to XML.Use Accept HTTP Header from Client to specify JSON or XML
Use the Accept: HTTP header to specify what format to return (Accept: application/json, Accept: text/xml). Note that JavaScript libraries like jQuery and Angular automatically POST data as application/json so you get JSON input typically.Continue to use the Request Object to get other Request info
You can still use Request.QueryString() and Request.Form() to parse non-JSON input data from URLs and it's also possible to return Response.Write() results by setting Service.IsRawResponse = .T. which doesn't serialize the result or tries to set the output content type.InitTokenSession() for a basic Bearer Token Generation and Tracking
You can use Web Connection'swwSession
object via theInitTokenSession()
method to create, track and validate Authorization Bearer tokens and make them accessible via theoSession
instance.Authenticate() for tying Bearer Tokens to wwUserSecurity User Records
In addition to wwSession support you can also map a Bearer Token to a user records usingwwUserSecurity
. By using theAuthenticate()
method to validate and authorize users, sessions and users are automatically linked in the same way they are for standard wwProcess HTML applications.
Remarks
Services methods expose the following PRIVATE variables that let you control lower level access to the server:
JsonService
The wwJsonService class that controls the JSON input and output parsing. This is useful to override behavior of the service, such as telling it to not generate any output using the IsRawResponse property which you can use if you want to use the Response object to return your own raw response output (ie. usingResponse.Write()
).Serializer
An instance of the wwJsonSerializer which allows you to override behavior of the serializer. For example, you can override things like the PropertyNameOverrides property to generate 'proper' field names rather than all lowercase.Request, Response, Server, Process, Session etc.
Of course all the other, normally exposed objects inside of process classes are also in scope so you can access any Request object methods like QueryString or ServerVariables() to retrieve input.
Class Members
Member | Description | |
---|---|---|
OnAfterCallMethod |
Hook method called after every REST service request. | |
OnBeforeCallMethod |
Hook method that allows you to into every REST service call before the call is made. | |
Authenticate |
The Authenticate method uses UserSecurity authorization to handle:o.wwRestProcess.Authenticate(lcUsername, lcPassword) |
|
CreateJsonService |
Hook class that is called to create an instance of wwJsonService which is responsible for making the actual method call. o.CreateJsonService() |
|
ErrorResponse |
Creates an error response formatted into the proper output format depending on the Accept header of the request. Defaults to JSON, if no Accept header is provided. o.ErrorResponse(lcMessage, lcStatus) |
|
InitTokenSession |
This method is a Bearer Token based implementation of the wwSession object that allows you track user state between REST client calls. Most commonly this can be used to map a Bearer Token to a user or customer record. o.wwRestProcess.InitTokenSession(llAutoSave, lnTimeout) |
|
lRawResponse |
Determines whether the current requests returns a raw response result rather than a JSON response. | |
oJsonService |
Instance of the wwJsonService object that's handling the request so that you can control request behaviors for the current request. |
See also:
Using wwRestProcess Class | How wwRestService works© West Wind Technologies, 1996-2024 • Updated: 11/15/23
Comment or report problem with topic