hub.eb?material_id=526&track_id=532

Handling an Endpoint Event


Endpoint scripts can be added to each endpoint and are executed when a request for the endpoint is processed.

Endpoint Scripts have access to form.rest methods.

The following form.rest methods can be used to access request data:

  • getMethod() – returns the HTTP method (GET, POST) used to call the endpoint.
  • getEndpointPath() – returns the endpoint path invoked that is received by the system.
  • getPathParameter(name) - returns the incoming path parameter value by name or returns null if the variable does not exist.
  • getPathParameters() - returns a Map of key, value String pairs for all the incoming path parameters.
  • getRequestBody() - returns the request body or null, if no body is set on the request.
  • getRequestContentType() - returns the Content-Type HTTP header from the incoming request. e.g application/json. Returns null if the Content-Type header is not set.
  • getRequestHeader(name)- returns the HTTP header value by name or returns null if the header does not exist.
  • getRequestHeaders() - returns a Map of key, value String pairs for all the incoming HTTP headers.
  • getRequestParameter(name) - returns the incoming query or post parameter value by name or returns null if the variable does not exist.
  • getRequestParameters() - returns a Map of key, value String pairs for all the incoming query/post parameter.

 

The following methods can be used to set information on the response:

 

  • setResponseBody(body) – sets the response body.
  • setResponseContentType(type) – sets the Content-Type HTTP header for the response e.g. application/json.
  • setResponseHeader(name, value) – adds a response HTTP header value by name.
  • setResponseStatus(status) – sets the response status code. The default is 200 - HTTP OK.

 

Errors should be handled within the endpoint event and the appropriate HTTP status should be set on the response before exiting the event.

If an error is uncaught when executing an endpoint event, then the On Error event is invoked, if configured. By default, unhandled errors return a status code of 500 (server error) and the server error message is returned in the response body.

Steps


1

Create a Public REST Web Service

2

Create a new REST Endpoint, click the  called newEndpoint.

3

Set the Endpoint HTTP methods to GET and POST.

4

Add an Endpoint Event Script called

5

Insert the following code:

switch (form.rest.getMethod()){
  case "GET":
    form.rest.setResponseBody({
      data: "T DATA",
      msg: "user data retrieved"
    });
    form.rest.setResponseStatus(200);
  break;
  case "POST":
    //some processing of request data
    form.rest.setResponseBody({
      msg: "new user created"
    });
    form.rest.setResponseStatus(200);
  break;
  default:
  form.rest.setResponseStatus("405");
  form.rest.setResponseBody({
    msg: "No HTTP Method specified or invalid HTTP Method used"
    })
  break;
}

 

Current Module

Related