Skip to content

REST Overview Guide

The following sections cover a general overview of hosting and implementing REST web services in Linx with the use of the RESTHost function.

  • The flow of a REST request
  • The data structure of REST request in Linx
  • Supported HTTP status codes
  • Supported Media Types

When a request is made to a RESTHost service operation, the following flow takes place:

  1. Input deserialization and validation: After a request is received, Linx performs deserialization and validation of the inputs (query string, request body).
  2. Built-in security validations: Linx then executes built-in .NET security validations according to the operation’s applied HTTP security scheme (Basic, Bearer or API Key).
  3. Authentication event: Authentication event which is executed for Basic and API Key authentication. This allows you to add custom authentication logic for these schemes.
  4. Before operation event: Optional event which allows you to add custom logic to requests, before the operation is executed.
  5. Execute operation: Executes the operation in which the custom logic for the endpoint is built.
  6. After operation event: Optional event which allows you to add custom logic to requests, after the operation has been executed, regardless of success.
  7. Output serialization: Serialization of the outputs returned from the operation.
  8. Error handler: The whole flow is encapsulated in a built-in error handler.

After a request is received, the incoming request’s parameters, such as the request body or query values, are deserialized and then validated according to the API definition.

If a validation error occurs, a 400 Bad Request response is returned along with a response body that contains the details of the invalid parameters.

Linx then executes built-in .NET security validations according to the operation’s applied HTTP security scheme.

Currently, the RESTHost service supports the following built-in security schemes:

  • HTTP Basic
  • HTTP Bearer
  • API Key

When a request is made to an operation that has one of the above security schemes applied to it, the following will happen:

  • Basic and API Key: Validate format (in header, in query etc.)
  • Bearer: Automatically verify JWT token

If unsuccessful, a 401 Unauthorized response is returned.

If successful, details associated with the authenticated user are passed through to the operation via:

  • For HTTP Basic and API Key security schemes, details are passed through to the operation in AuthenticationData.
  • For the HTTP Bearer security scheme, details are passed through to the operation in HTTPContext.User.

OperationEvents_Authenticate

The authentication event is executed for HTTP Basic and API Key authentication schemes. You must add custom authentication logic in this event to handle the verification of Basic and API Key authentication credentials. An example of such logic would be confirming the validity of a API Key from a database.

By default, this event returns $.Result.Data.HttpContext.User.IsAuthenticated = False, so you must add logic in this event to indicate that authentication was successful. If authentication fails, a 401 Unauthorized response is returned.

OperationEvents_BeforeOperation

You can add custom logic to this event, which will execute before the operation is executed. This applies to all operations and can be used for things such as custom logging, throttling, or additional custom authentication.

The operation that contains your custom logic is executed once all necessary validations have occurred.

By default, all operations are set to have a default response of 200 OK. If you want to return a different HTTP status code, such as 201 or 402, you can by setting a custom response.

OperationEvents_AfterOperation

You can add custom logic to this event, which will fire after any operation has executed, regardless of the result.

The outgoing response properties are deserialized according to the API specification.

The whole flow is encapsulated in a built-in error handler. If an error occurs at any stage in the flow, a 500 Internal Server Error will be returned. You have the option of including or omitting error details from the response by altering the Return server errors of the RESTHost service to True.


When a request is received or a response is returned, it is structured as follows:

Highest-level request/response object.

DataTypeIncluded in
HTTPContexthttpContextRequests/Responses
AuthenticationDataauthenticationDataRequests with security events
Input ParametersinputParameterRequests
ResponseXXXresponseXXXResponses

Object containing all metadata and additional information associated with the request.

HTTPContextTypeExample
Connectionconnection
ContentLengthInteger12322
ContentTypeStringApplication/json
CookiesListx-api-key=aadedwde
HeadersList
HostStringlocalhost
IsHttpsBooleantrue
ItemsList[‘User1’,‘User2’]
MethodStringGET
PathString/users
PathBaseString/LinxSampleAPI/restHOST
ProtocolStringHTTP/2
QueryList
QueryStringString?x-api-key= xsdshsdj37sh
SchemeStringhttps
Useruser

Connection details associated with the source of the request.

ConnectionTypeExample
IDString-485183435770848
LocalIpAddressString::1
LocalPortInteger443
RemoteIpAddressString::1
RemotePortString55023

Headers are used to pass additional information with the request. Typical usage involves the Authorization header used for authentication schemes such as Basic, Bearer or API Key.

HeaderTypeExample
KeyStringAuthorization
ValueList<String>Bearer Xhsjjs73890s0

Query values are used to pass additional information usually related to the request such as a filter value.

These are passed in with the HTTPContext.QueryString of the request URL, they are then deserialized into the following format:

QueryTypeExample
KeyStringx-api-key
ValueList<String>[‘xsdshsdj37sh’]

Contains information related to the authenticated user from the built-in security validations as well as from the OperationEvents_Authenticate event.

You are able to alter this property in order to pass data through to the subsequent events and operations.

An example of this could be if you validate an API key with custom logic in the OperationEvents_Authenticate event by matching the key to a user, you can then return the user’s id from the database which can be passed on to the main operation. This avoids re-extracting the identifiers that might be of use in the operation.

For the HTTP Bearer authentication scheme, the unique_name claim value is extracted from the JWT token and passed in with the User.Name property.

UserTypeExample
AuthenticationTypeString2
IsAuthenticatedBooleantrue
NameString2233

When parameters are passed in with a request in either the request URL, query string or request body, they are deserialized and accessible in the $.Input.Data property of the operation and events:

Input ParameterTypeExample
URL parametersString/users/45
Query valuesStringName=John
Header valuesStringName=John
CookiesStringName=John
Request BodyText/XML/JSON/Filestream/URL Encoded{“user”: {“name”: “John”}}


ResponseXXX

By default, when an operation completes, a (200) OK response status code with a response body of no-content is returned. However, you are also able to return custom response codes as well as custom response content which can be basic types or objects containing nested types and objects.

OutputType
Response BodyJSON/XML/Binary


AuthenticationData

When the built-in security validations have succeeded, details relating to the authenticated user will be passed on to any operations and events down the request flow.

AuthenticationDataTypeExample
SchemeNameStringBasicAuth
SchemeTypeschemetype1
ApiKeyStringApiKey

The API Key object will contain the below information if the security scheme for the operation is API Key. If the authentication is HTTP Basic, then this object will be NULL.

ApiKeyTypeExample
TokenLocationtokenLocation2
TokenNameStringX-API-KEY
ProvidedTokenString0imfnc8mVLWwsAawjYr4Rx-Af50DDqtlx

The following HTTP Status Codes are supported in the RESTHost operation responses.

Built-in

Status CodePhraseUse
200OKDefault response code for all operations unless set otherwise.
401UnauthorizedDefault response code for security validation failures or authentication failures.
400Bad RequestDefault response for incorrect structure of request.
415Unsupported Media TypeDefault response for unsupported media in the content-type header.
500Internal Server ErrorDefault response code when an unhandled exception occurs in the request flow. Users are able to include or omit the server errors from the response.

Custom

Status CodePhraseUse
1XXInformationalCustom Response
2XXSuccessCustom Response
3XXRedirectionCustom Response
4XXClient ErrorCustom Response
5XXServer ErrorCustom Response

In the case of invalid authentication credentials or submission format, a (401) Unauthorized response is returned.

If you alter the $.Result.Data.HTTPContext.StatusCode to anything (even 200) within this event’s custom logic, a response with the code will be returned and the request flow will cease.

The OperationEvents_Authenticate event is set by default to return a result value of $.Result.Data.HtttpContext.User.IsAuthenticated which triggers a (401) Unauthorized response.

Therefore, you must add custom logic in this event to set the value of $.Result.Data.HtttpContext.User.IsAuthenticated in the case of HTTP Basic and API Key security schemes.

By default, all operations have a response Status Code of (200) OK unless set otherwise or an exception is thrown within the operation’s logic; in that case, a (500) Internal Server Error is returned.

If you want to return a custom response code like 201 or 404, then you are also able to configure it in your operation logic. This is done by adding the relevant HTTP Response Codes to your API definition, and then setting the $.Result.Data.HTTPContext.StatusCode property within the operation to the code you require. Following this, you can set the relevant response body.

You are able to override the HTTPContext.StatusCode of the operation in this event; you are able to set it as any response code.


Input parameters are deserialized into the relevant TYP.

The following data/media types are supported in requests:

Input Data TypeLocationAccepted Content-Type Header
Basic Data Type (integer, string etc)URL, Query, Body- application/json
- text
- application/xml
Object, ListBody- application/json
- application/xml
Binary / FilestreamBody- application/octet-stream

If you attempt to make a request with an incorrect content-type header or an unsupported content-type, then a (415) Unsupported Media Type response will be returned automatically.

View our sample solution on GitHub.