com.WebServiceEngine.GetHTTPServiceRequest

Get a handle for an incoming HTTP service request.

Syntax

com.WebServiceEngine.GetHTTPServiceRequest(
   timeout INTEGER)
  RETURNS com.HttpServiceRequest
  1. timeout defines the timeout in seconds.

It returns a com.HttpServiceRequest object for an incoming request, and NULL when no request arrives within the timeout. Errors are raised as exceptions: for the list, go to Table 1.

Usage

The com.WebServiceEngine.GetHTTPServiceRequest() class method waits for an incoming HTTP request. Use a timeout of -1 to wait without a time limit.

You must handle or destroy the previous request before calling the method again. Otherwise, the method raises an error.

The method raises errors as exceptions, which you trap with TRY/CATCH. The sqlca.sqlerrm register can hold a description of the problem.

Table 1. GetHTTPServiceRequest exceptions
Error code Situation Resolution/Additional notes
-15552 The server cannot convert a UTF-8 URL to the runtime (fglrun) locale charset Run the Web Services server program in UTF-8.
-15565 The request is not valid HTTP: a malformed request line, an unknown method, an unsupported HTTP version, or a URL path that cannot be decoded

Starting from FGLGWS 6.00.03, the server responds to the client with an HTTP error status (501 for an unknown method, 505 for an unsupported HTTP version, and 400 for other invalid requests) and keeps running, so the application can process later requests.

In earlier versions, such a request puts the GWS HTTP server into an invalid state. Even if the application traps the exception, every later call fails with the same error. The application must then shut down cleanly and exit.

-15575 The Genero Application Server disconnected the Web Services program Exit the program.
Note:

In earlier versions, with GAS 6.00.04 or later, this is not an issue in production environments: the Genero Application Server (GAS) and GWSProxy detect the ended DVM, return an HTTP error code to the client app, and any new request starts a new DVM in a clean state through GWSProxy and the pool configuration.

Example

This example demonstrates using com.WebServiceEngine.getHTTPServiceRequest(10) to handle incoming requests. In the WHILE loop, if no request arrives within 10 seconds the call returns NULL and the current time is output. When a request arrives (for example, curl localhost:8090/service_request), it replies with 200 and includes the request URL. The loop is wrapped in a TRY/CATCH to handle disconnects, using the status variable to report errors (status == -15575).

Key points:

  • getHTTPServiceRequest(10): waits up to 10 seconds (use -1 to wait indefinitely).
  • req IS NULL: timeout heartbeat (prints time).
  • req.sendTextResponse(..., req.getUrl()): sends 200 response including the request URL.
# service_request.4gl
IMPORT com

MAIN
  DEFINE req com.HttpServiceRequest
  DISPLAY "Server started"
  CALL com.WebServiceEngine.Start()
  TRY
    WHILE TRUE
      LET req = com.WebServiceEngine.getHTTPServiceRequest(10)
      IF req IS NULL THEN
        DISPLAY "HTTP request timeout...: ", CURRENT YEAR TO FRACTION
      ELSE
         DISPLAY "Request URL is :",req.getURL()
         CALL req.sendTextResponse(200,NULL,SFMT("It works (%1)", req.getUrl()))
      END IF
    END WHILE
    CATCH
      IF status == -15575 THEN
        DISPLAY "Disconnected : ",sqlca.sqlerrm
      ELSE
        DISPLAY SFMT("ERROR : %1 - %2",status,sqlca.sqlerrm)
      END IF
  END TRY
END MAIN