It's now 2021 and no there's no native way to do this. However all you need to call REST Services is:
- An HTTP Client
- A JSON Serializer
You can create a very simple WinHttp HTTP client using FoxPro code (or some of the XmlHttp code shown in other anwers in this thread):
FUNCTION WinHttp(lcUrl, lcVerb, lcPostData, lcContentType)
LOCAL lcResult
*** FOR DEMOS ONLY!
IF EMPTY(lcUrl)
RETURN null
ENDIF
IF EMPTY(lcVerb)
lcVerb = "GET"
IF !EMPTY(lcPostData)
lcVerb = "POST"
ENDIF
ENDIF
*** Example of using simplistic WinHttp client to retreive HTTP content
LOCAL loHttp as WinHttp.WinHttpRequest.5.1, lcResult
loHTTP = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
loHTTP.Open(lcVerb, lcUrl,.F.)
IF !EMPTY(lcContentType) AND lcVerb = "POST" OR lcVerb = "PUT"
loHttp.SetRequestHeader("Content-Type",lcContentType)
ENDIF
*** If using POST you can post content as a parameter
IF !EMPTY(lcPostData)
loHTTP.Send(lcPostData)
ELSE
loHttp.Send()
ENDIF
lcResult = loHttp.ResponseText
loHttp = NULL
RETURN lcResult
This is pretty basic code without error correction but you can go from there.
lcResult = WinHttp("https://albumviewer.west-wind.com/api/artist/1")
? lcResult && JSON response
*** Create some JSON to post manually
TEXT TO lcJson NOSHOW
{
"username": "test",
"password": "test"
}
ENDTEXT
lcResult = WinHttp("https://albumviewer.west-wind.com/api/authenticate","POST",lcJson,"application/json")
? lcResult && JSON
For JSON serialization and parsing you need to use a library of some sort. Here are a couple FoxPro JSON libraries that are still maintained:
To put together wwHttp
and wwJsonSerializer
manually looks something like this to make a POST
request and receive a JSON result as an object:
LOCAL loHttp as wwHttp, loSer as wwJsonSerializer
loHttp = CREATEOBJECT("wwHttp")
loSer = CREATEOBJECT("wwJsonSerializer")
loUser = CREATEOBJECT("EMPTY")
ADDPROPERTY(loUser,"Username", "test")
ADDPROPERTY(loUser, "Password", "test")
lcJson = loSer.Serialize(loUser)
loHttp.cContentType = "application/json"
lcJson = loHttp.Post("https://albumviewer.west-wind.com/api/authenticate", lcJson)
IF loHttp.nError # 0
? "Failed: " + loHttp.cErrorMsg
ENDIF
IF loHttp.cResultCode = "401"
? "Login failed. Invalid credentials"
RETURN
ENDIF
IF loHttp.cResultCode # "200"
? "Failed: " + loHttp.cResultCode + " " + loHttp.cResultCodeMessage
RETURN
ENDIF
loAuth = loSer.Deserialize(lcJson)
lcToken = loAuth.Token && JSON Object contains token
IF EMPTY(lcToken)
? "Authentication failed. Invalid token."
RETURN
ENDIF
If you need more detail and background there's a very detailed white paper you can check out here that talks both about the client and server sides for FoxPro specific scenarios: