Consuming RESTful Services in DataFlex: Difference between revisions

From DataFlex Wiki
Jump to navigationJump to search
Keeping backwards compatibility for DF19.x on de demo code
m (Typo)
(Keeping backwards compatibility for DF19.x on de demo code)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[File:The RESTTest view.jpg|thumb|100px|The RESTTest view]]
[[File:The RESTTest view.jpg|thumb|100px|The RESTTest view]]


To consume a web service we are going to need to be able to make HTTP requests and receive their responses. The way of doing this in DataFlex is by using the [https://docs.dataaccess.com/dataflexhelp/index.htm#t=mergedProjects%2FVDFClassRef%2FcHttpTransfer.htm cHttpTransfer class] (or its sub-classes: the [https://docs.dataaccess.com/dataflexhelp/index.htm#t=mergedProjects%2FVDFClassRef%2FcJsonHttpTransfer.htm cJsonHttpTransfer class] and the [https://docs.dataaccess.com/dataflexhelp/index.htm#t=mergedProjects%2FVDFClassRef%2FcXmlHttpTransfer.htm cXmlHttpTransfer class]).
To consume (be a client of) a web service we are going to need to be able to make HTTP requests and receive their responses. The way of doing this in DataFlex is by using the [https://docs.dataaccess.com/dataflexhelp/index.htm#t=mergedProjects%2FVDFClassRef%2FcHttpTransfer.htm cHttpTransfer class] (or its sub-classes: the [https://docs.dataaccess.com/dataflexhelp/index.htm#t=mergedProjects%2FVDFClassRef%2FcJsonHttpTransfer.htm cJsonHttpTransfer class] and the [https://docs.dataaccess.com/dataflexhelp/index.htm#t=mergedProjects%2FVDFClassRef%2FcXmlHttpTransfer.htm cXmlHttpTransfer class]).


We will also want to make use of objects of the [https://docs.dataaccess.com/dataflexhelp/index.htm#t=mergedProjects%2FVDFClassRef%2FcJsonObject.htm&rhsearch=cJsonObject&rhhlterm=cJsonObject&rhsyns=%20 cJsonObject class] to handle responses and, in cases where we wish to create or update data, handle our request contents.
We will also want to make use of objects of the [https://docs.dataaccess.com/dataflexhelp/index.htm#t=mergedProjects%2FVDFClassRef%2FcJsonObject.htm&rhsearch=cJsonObject&rhhlterm=cJsonObject&rhsyns=%20 cJsonObject class] to handle responses and, in cases where we wish to create or update data, handle our request contents.
Line 10: Line 10:


*File --> New --> View / Report --> Data Entry View
*File --> New --> View / Report --> Data Entry View
*Name the object: "'''oRESTTest'''"
*Name the file: "'''RESTTest'''"
*Double click on the view to switch to the code editor
*Double click on the view to switch to the code editor
*Select all (Ctrl-A), then paste in the code below, overwriting what is there
*Select all (Ctrl-A), then paste in the code below, overwriting what is there
Line 16: Line 16:
*In the program, do View --> RESTTest to open the view and see it in action (just click the "Send" button with the defaults for a first look to check that all is working properly).
*In the program, do View --> RESTTest to open the view and see it in action (just click the "Send" button with the defaults for a first look to check that all is working properly).


'''RESTTest.vw''':
<source lang="vdf">
<source lang="vdf">
Use Windows.pkg
Use Windows.pkg
Line 106: Line 107:
             Move (Value(oCreds) + ":" + Value(oPassword)) to sToken
             Move (Value(oCreds) + ":" + Value(oPassword)) to sToken
             Move (Base64Encode(AddressOf(sToken), Length(sToken))) to pCreds
             Move (Base64Encode(AddressOf(sToken), Length(sToken))) to pCreds
             Move pCreds to sToken
#IF (!@ < 200)
             Move pCreds to sToken  
#ELSE
            Move (PointerToString(pCreds))  to sToken
#ENDIF
             Move (Free(pCreds)) to bOK
             Move (Free(pCreds)) to bOK
             Get AddHeader of oHttp "Authorization" ("Basic" * sToken) to bOK
             Get AddHeader of oHttp "Authorization" ("Basic" * sToken) to bOK
Line 423: Line 428:
By default the view is set to call http://jsonplaceholder.typicode.com/posts (click that link to see the results you should get), which is a simple testing (somewhat) RESTful web service (you can use a path of "posts" - the default - "todos", "comments", "albums", "photos" or "users", and use POST, PUT, PATCH and DELETE, as well as the default GET - see http://jsonplaceholder.typicode.com for all the options). It supports both HTTP and HTTPS. Calls will not change anything, but do return sensible responses. It is useful for testing.
By default the view is set to call http://jsonplaceholder.typicode.com/posts (click that link to see the results you should get), which is a simple testing (somewhat) RESTful web service (you can use a path of "posts" - the default - "todos", "comments", "albums", "photos" or "users", and use POST, PUT, PATCH and DELETE, as well as the default GET - see http://jsonplaceholder.typicode.com for all the options). It supports both HTTP and HTTPS. Calls will not change anything, but do return sensible responses. It is useful for testing.


To test other services, simply change the inputs to what they require.
To test other services, simply change the inputs to what those require.


==See also==
==See also==


*[[REST]]
*[[RESTful Service Theory]]
*[[RESTful Service Theory]]
*[[Creating RESTful Services in DataFlex]]
*[[Creating RESTful Services in DataFlex]]
Line 432: Line 438:
*[[Using the REST Library]]
*[[Using the REST Library]]


[[Category:REST]]
[[Category:REST]][[Category:Web Services]]

Navigation menu