KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A utility to extract the URL from HTTP GET and POST requests
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Mac & Win
Published On: September 25, 2014

For proper web request processing it is important to know what type message, GET or POST, is received to know how to properly extract the net URL from the string contained in the $1 parameter of the Database method On Web Connection.

HTTP Header for a POST request
When a POST method is received, shown below, any web query string is contained at the bottom of the header. Only the leading forward slash "/" need be removed. In the example below only " /newResultsPage" is passed in the $1 parameter of the On Web Connection method.

POST /newResultsPage HTTP/1.1
...
X-Requested-With: XMLHttpRequest

srchKey=srchValue


HTTP Header for a GET request
When a GET method is received, shown below, any web query string is appended to the URL, preceeded by a question mark "?." In addition to the leading forward slash "/" needing removal, only the text preceeding the question mark should be returned as the URL. The remaining text is actually the Web Variables that populate the web variable names and values arrays my the command WEB GET VARIABLES. In the example below " /newResultsPage?srchKey=srchValue" is passed in the $1 parameter of the On Web Connection method.
GET /newResultsPage?srchKey=srchValue HTTP/1.1
...
X-Requested-With: XMLHttpRequest


Project method -- UTIL_Extract_URL


If (True)
    If (False)
      Begin SQL
      /*
      Name: UTIL_Extract_URL
      Path: UTIL_Extract_URL

      Purpose: Extract the URL from a POST or GET request

      $0 - TEXT - The URL string minus additional text
      $1 - TEXT - The whole URL string
      */
      End SQL
    End if
    C_TEXT($MethodName_T)
    $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
    C_TEXT($0;$URL_T;$1)
    //---------------------------------------------------------------------------
    //method_wide_constants_declarations
    //---------------------------------------------------------------------------
    //local_variable_declarations
    C_LONGINT($Ndx;$Params_L)
End if
//====================== Initialize and Setup ================================

$0:=""
$Params_L:=Count parameters
If ($Params_L>=1)
   $URL_T:=$1

   If (Length($URL_T)>0)
       //=================== Method Actions =============================

       // Remove the leading "/" if there is one
       //
       If (Position("/";$URL_T)=1)
         $URL_T:=Delete string($URL_T;1;1)
       End if

       // Incase the request is a GET instead of a POST
       // The URL preceeds the query string separator, "?"
       //
       $Ndx:=Position("?";$URL_T)
       If ($Ndx>0)
         $URL_T:=Substring($URL_T;1;$Ndx-1)
       End if

       //=================== Clean up and Exit ============================
    End if

End if

$0:=$URL_T