4

I have created a RestFUL api using mvc4 asp.net web api, one of my customers needs to consume some services from his visual fox pro system.

He tells me can't use my RestFUL services, because there's not way. I don't know anything about Visual FoxPro.

Is there any way to consume RestFUL services from visual fox pro?

ngonzalez
  • 171
  • 3
  • 6
  • By strict definition the answer is no. However, there are ways you could engineer this behavior. Do you know if the database is SQL Server or Native? – David C Jan 09 '13 at 17:17
  • RestFUL services are using sql azure and Visual FoxPro system is using native database. It is mandatory to use RestFUL services, as far as I know there is an option using WinHttpRequest object, we are trying to call RestFUL servicies with this object. – ngonzalez Jan 09 '13 at 19:24

4 Answers4

2

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:

Rick Strahl
  • 17,302
  • 14
  • 89
  • 134
1

You're really asking two questions here.

1: "Is there any native functionality to read RESTful web services in Visual FoxPro?"

No. Meaningful development of Visual FoxPro ceased before web services moved from a quirky browser thing to a central technology, and so this legacy xBase system was never adjusted to be able to communicate them.

2: "Is there any way at all to get data from a RESTful web service into Visual FoxPro?"

Yes. VFP can act as both a COM or ODBC client, and can harness data relayed from either of those methods. Provided that you don't encounter a security wall and have sufficient skill to manually specify the HTTP headers, the standard(1) MSXML.XMLhttpRequest object is your best choice.

Be aware that you won't be able to natively consume JSON with FoxPro, although there was at least one library to convert from JSON to a VFP-usable object. XML is preferable if you have a choice, as you may be able to leverage the XMLAdapater class introduced in the last version of Visual FoxPro.

(1: "Standard" here meaning, "standard on the windows platform." Anything allowed to run Visual FoxPro that should be allowed anywhere near the internet has MSXML already installed.)

DougM
  • 2,808
  • 17
  • 14
1

Although there is no "native" way to call REST services from VFP, but you can easily create a MSXML2.ServerXMLHTTP object, which comes with Windows. I've only used the MSSoap.SoapClient30 to make SOAP calls (which you must install), but I prefer the MSXML2.ServerXMLHTTP object for REST calls.

Here is an example of how to call a REST service through VFP.

Example of making REST GET method call

xmlHttp = CREATEOBJECT("MSXML2.ServerXMLHTTP")
xmlHttp.open("GET", "put url with parameters here", null)
xmlHttp.setRequestHeader("Content-Type", "text/xml")
xmlHttp.send("")
result = xmlHttp.responseText
? result

Example of making REST POST method call

xmlHttp = CREATEOBJECT("MSXML2.ServerXMLHTTP")

xmlHttp.open("POST", "put URL here", null)
xmlHttp.setRequestHeader("Content-Type", "text/xml")
xmlHttp.send("put string to send in body")
result = xmlHttp.responseText
? result
0

Yes,

FoxPro can use Mircrsoft Soap SDK software to connect to any WebServervice, this one, is preferible that returns a XML.

This is what I use, after install free software "soapsdk.exe"

local loWS, loP

loWS=createobject("mssoap.soapclient30")    

loWS.mssoapinit("http://nn.nn.nnn.nn:80/Default.asmx?WSDL")

loP=loWS.name_public_method(html_param_1,html_param_2...)

loP contains can cointain result in XML format

Albert Català
  • 2,026
  • 2
  • 29
  • 35