Friday, September 25, 2026

New VFPX Project: VFPREST

REST services, sometimes called RESTful web services, are a lightweight and scalable way to design networked applications that communicate over HTTP using standard methods like GET, POST, PUT, and DELETE. There are thousands of services available as REST APIs, everything from UPS package tracking to address validation to currency conversion. The Public APIs site alone lists over 1,500 free APIs.

There are lots of ways to make a REST API call in VFP applications:
  • WinHttp.WinHttpRequest, which is built into Windows.

  • West Wind Technologies' wwHTTP class, part of West Wind Internet & Client Tools and West Wind Web Connection.

  • Chilkat Software’s Chilkat utility. Bill Anderson has a wrapper class that makes using Chilkat from VFP easier.

  • CURL, a command-line utility that comes with Windows for transferring data to or from a server via a URL.
However, regardless of which mechanism you choose, there's still lots of complex code to write.

VFPREST is a new VFPX project that makes this job much easier. For simple REST APIs, just subclass the BaseREST class, fill in some properties, and add a method that calls the API function.

Here's an example of a class that accesses Pet Store, an API test site. It has two methods: AddPet to add a pet and GetPet to get information about a pet.
define class PetStoreAPI as BaseREST of BaseREST.prg

* Override parent properties.

	cURL = 'https://petstore.swagger.io/v2/'

* Custom properties.

	protected cAPIKey
	cAPIKey = 'special-key'
	
	nID     = 0
		&& the pet ID
	cName   = ''
		&& the pet name
	cStatus = ''
		&& the pet's status

* Add a pet to the store.

	function AddPet(toParameter)
		This.AddQueryString('apikey', This.cAPIKey)
		llReturn = This.APICall('POST', '200', 'pet', toParameter)
		if llReturn
			This.nID = This.oResponse.ID
		endif llReturn
		return llReturn
	endfunc

* Find a pet.

	function GetPet(tnPetID)
		This.AddQueryString('apikey', This.cAPIKey)
		llReturn = This.APICall('GET', '200', 'pet/' + transform(tnPetID))
		if llReturn
			This.cName   = This.oResponse.Name
			This.cStatus = This.oResponse.Status
		endif llReturn
		return llReturn
	endfunc

* Handle errors.

	protected function HandleErrors(toJSON)
		if pemstatus(toJSON, 'type', 5) and toJSON.type = 'error'
			This.cErrorMessage = toJSON.message
		endif pemstatus(toJSON, 'type', 5) ...
	endfunc
enddefine

(Another class that provides the pet information passed to AddPet isn't shown here for brevity.)

You can download VFPREST from https://github.com/DougHennig/VFPREST or add it to a project using FoxGet.

I'll be showing VFPREST at Virtual FoxFest 2026 so that's yet another reason to attend.

No comments: