KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Check if Resource Exists on Web Server Without Downloading
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: October 18, 2021

You can check if a certain resource exists on the web server by making an HTTP request to the server and specifying the path to the resource. For example, to check whether the picture file named pic.jpeg exists on the web server, the HTTP request would be formmated as such: http://localhost/pic.jpeg



If you are just checking for whether the file exists, you will want to use the HEAD method in your HTTP request. The HEAD method is similar to the GET method except it only returns the header as a response and does not return the message body. If the file size is large, using HEAD instead of GET will help save a lot of time and resources, since the content does not need to be returned in the response.

The utility method below can be used to check for whether a certain resource exists on the web server. When calling this method, pass the URL path to the resource in the first parameter.

//============================
//METHOD: util_doesWebResourceExist
//DESCRIPTION: Checks if resource exists on the server
// $1 (Text) - Accepts the URL path to the resource on the server
// $0 (Boolean) - Returns True or False for whether the resource was found at the provided URL
//============================
If (Count parameters=1)
  C_TEXT($1;$url_t;$content_t;$response_t)
  C_BOOLEAN($0;$exists_b)
  $url_t:=$1
  
  $HTTPStatus_l:=HTTP Request(HTTP HEAD method;$url_t;$content_t;$response_t)
  If ($HTTPStatus_l=200)
    $exists_b:=True
  Else
    $exists_b:=False
  End if
  
  $0:=$exists_b
End if

Example of calling this method.
C_BOOLEAN($exists_b)
$exists_b:=util_doesWebResourceExist("http://127.0.0.1/pic.jpeg") //$exists_b = True