A Simple RESTful Service: Difference between revisions

From DataFlex Wiki
Jump to navigationJump to search
m (Link)
 
(6 intermediate revisions by one other user not shown)
Line 1: Line 1:
To create a service we will require a WebApp, and within that an object of the [https://docs.dataaccess.com/dataflexhelp/index.htm#t=mergedProjects%2FVDFClassRef%2FcWebHttpHandler.htm&rhsearch=cWebHttphandler&rhsyns=%20&rhhlterm=cWebHttphandler cWebHttpHandler class].
To create a service we will require a WebApp, and within that an object of the [https://docs.dataaccess.com/dataflexhelp/index.htm#t=mergedProjects%2FVDFClassRef%2FcWebHttpHandler.htm&rhsearch=cWebHttphandler&rhsyns=%20&rhhlterm=cWebHttphandler cWebHttpHandler class].


The code below shows how you might write a simple RESTful API for the sample Customer table (from the DataFlex Examples: WebOrder or WebOrderMobile, which will, by default, have been installed under "C:\DataFlex ''nn''.''n'' Examples" if you chose to install the Examples during the installation process - it also uses the WebAppUser table from those examples).
The code below shows how you might write a simple RESTful API for the sample Customer table (from the DataFlex Examples: WebOrder or WebOrderMobile, which will, by default, have been installed under "C:\DataFlex ''nn''.''n'' Examples" if you chose to install the Examples during the DataFlex installation process - it also uses the WebAppUser table from those examples).


'''Warning''': This sample code exposes your customer table to the world (or, if using one of the security options below, to anybody who has valid credentials) - they can abuse that as they choose. It is probably not an approach you would want to take in a real-world environment without considerable enhancement! (Details on the [[Talk:A_Simple_RESTful_Service|discussion page]].)
'''Warning''': This sample code exposes your customer table to the world (or, if using one of the security options below, to anybody who has valid credentials) - they can abuse that as they choose. It is probably not an approach you would want to take in a real-world environment without considerable enhancement! (Details on the [[Talk:A_Simple_RESTful_Service|discussion page]].)


CustomerService.wo:
'''CustomerService.wo''':
<source lang="vdf">
<source lang="vdf">
Use cWebHttpHandler.pkg
Use cWebHttpHandler.pkg
Line 585: Line 585:
You should be able to copy and paste this code to see it in operation, as follows:
You should be able to copy and paste this code to see it in operation, as follows:


Your workspace should contain the tables Customer (perhaps copied from the WebOrder sample) and WebAppUser.
Your workspace should contain the tables Customer (perhaps copied from the WebOrder sample) and WebAppUser. (You may find that if you have only copied the Data and DDSrc directories from the samples that you need to comment out the line "Use CustomerWebLookup.wo" in the cCustomerDataDictionary.dd file in order to get things to compile.)


If your workspace does not already have a WebApp, in the Studio use File --> New --> Project --> Basic Web Project to create one first. Then with the WebApp as the current project (selector top-left on the Studio tool-bar by default), use File --> New --> Web Object --> WebHttpHandler, calling the object '''oCustomerService'''; then select all (CTRL-A) and paste the code above over the existing contents.
If your workspace does not already have a WebApp, in the Studio use File --> New --> Project --> Basic Web Project (or Desktop Web Project, if you prefer) to create one first.


If you then compile and run the WebApp (F5), and change the URL in the web browser (Firefox is best for this, as it displays the returned JSON in the nicest format), replacing "Index.html" with "customerAPI" (the psPath setting in the code) you should see the output produced by the "ApiRoot" procedure, which will (at this point) have a single ''collection'': "customers". Clicking (in Firefox, at least) on the "href" value for that should then display a list of customers, each of which should in turn have an "href" element, clicking on which will take you to the details for that customer.
Then with the WebApp as the current project (selector top-left on the Studio tool-bar by default), use File --> New --> Web Object --> WebHttpHandler, calling the object '''oCustomerService'''; then select all (CTRL-A) and paste the code above over the existing contents.
 
If you then compile and run the WebApp (F5), and change the URL in the web browser (Firefox is best for this, as it displays the returned JSON in the nicest format), replacing "Index.html" with "customerAPI" (the psPath setting in the code) you should see the output produced by the "ApiRoot" procedure, which will (at this point) have a link to a single ''collection'': "customers". Clicking (in Firefox, at least) on the "href" value for that should then display a list of customers, each of which should in turn have an "href" element, clicking on which will take you to the details for that customer (the ''instance'').


To do more than simply display (i.e. GET) data, you will have to use a tool such as [https://www.getpostman.com/ Postman] (click the "Get Started" button, then the "Download" button, selecting the version - 32 or 64 bit - as appropriate for your machine, saving and then running the downloaded file) or the RESTTester tool detailed in the [[Consuming RESTful Services in DataFlex]] article.
To do more than simply display (i.e. GET) data, you will have to use a tool such as [https://www.getpostman.com/ Postman] (click the "Get Started" button, then the "Download" button, selecting the version - 32 or 64 bit - as appropriate for your machine, saving and then running the downloaded file) or the RESTTester tool detailed in the [[Consuming RESTful Services in DataFlex]] article.
Line 601: Line 603:
</source>
</source>


To modify (update) an existing customer you will need to use PATCH to that customer's ''instance'' (e.g.: .../customerAPI/customers/101), passing the JSON to update it with, e.g.:
To modify (update) an existing customer you will need to use PATCH to that customer's ''instance'' (e.g.: .../customerAPI/customers/101), passing the [[JSON]] to update it with, e.g.:
<source lang="JSON">
<source lang="JSON">
{
{
Line 627: Line 629:
==See also==
==See also==


*[[REST]]
*[[RESTful Service Theory]]
*[[RESTful Service Theory]]
*[[RESTful Services in DataFlex]]
*[[RESTful Services in DataFlex]]
Line 632: Line 635:
*[[Creating RESTful Services in DataFlex]]
*[[Creating RESTful Services in DataFlex]]
*[[Using the REST Library]]
*[[Using the REST Library]]
* https://support.dataaccess.com/Forums/showthread.php?59025-RESTGen


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

Navigation menu