{"id":235,"date":"2016-03-02T08:35:14","date_gmt":"2016-03-02T13:35:14","guid":{"rendered":"http:\/\/salzlechner.com\/dev\/?p=235"},"modified":"2016-03-11T06:54:37","modified_gmt":"2016-03-11T11:54:37","slug":"interfacing-dataflex-restful-web-services","status":"publish","type":"post","link":"http:\/\/salzlechner.com\/dev\/2016\/03\/02\/interfacing-dataflex-restful-web-services\/","title":{"rendered":"Interfacing DataFlex &#8211; RESTful Web Services"},"content":{"rendered":"<h2>RESTful Web Service?<\/h2>\n<p>RESTful web services (representational state transfer) is a different architecture for web services than SOAP. RESTful services have a lot less transmission overhead and in general are a lot easier to create and consume.<\/p>\n<p>Most development is going the direction of RESTful services over SOAP.<\/p>\n<p>DataFlex currently only supports SOAP web services out of the box. In this blog post we show an example on how to call a RESTful service.<\/p>\n<p>RESTful services use the standard HTTP protocol (the same we have to view pages on the internet).<\/p>\n<h2>HTTP Verbs<\/h2>\n<p>The HTTP protocol uses so-called verbs to tell the server what type of request it is making. For example a GET request will tell the server that data is requested. A POST request is used to post data to the server. There are other verbs such as PUT, DELETE, etc.<\/p>\n<p>In order to show how to call a simple RESTful web service we built a small sample service using Visual Studio and Microsofts\u00a0Web API. The sample service has the following URL<\/p>\n<p>http:\/\/localhost\/api\/customer<\/p>\n<p>we implemented the GET method so calling the above URL will return all customers in the database<\/p>\n<p>Our customer database is actually an in memory cached data set that simply has an Id and a Name field.<\/p>\n<p>we also implemented a POST method so we can create a simple HTML form that allows the user to enter a new customer<\/p>\n<p>here is what our simple HTML page looks like<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipage.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-236\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipage.png\" alt=\"webapipage\" width=\"362\" height=\"345\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipage.png 362w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipage-300x286.png 300w\" sizes=\"(max-width: 362px) 100vw, 362px\" \/><\/a><\/p>\n<p>And here is the javascript code that allows is to get the list of customers and save a customer<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">$(function()\r\n{\r\n        $.getJSON('\/api\/customer', function(customersJsonPayload)\r\n        {\r\n            $(customersJsonPayload).each(function(i, item)\r\n            {\r\n                $('#customers').append('&lt;li&gt;' + item.Name + '&lt;\/li&gt;');\r\n            });\r\n        });\r\n});\r\n\r\n$('#saveCustomer').click(function () {\r\n    $.post(\"api\/customer\",\r\n            $(\"#saveCustomerForm\").serialize(),\r\n            function (value) {\r\n                $('#customers').append('&lt;li&gt;' + value.Name + '&lt;\/li&gt;');\r\n            },\r\n            \"json\"\r\n    );\r\n});\r\n<\/pre>\n<p>this all works fine of course and is not the part we are really interested in.<\/p>\n<p>What is interesting is how can we call a RESTful web service from a DataFlex application<\/p>\n<p>As mentioned earlier RESTful web services simply use the HTTP protocol. DataFlex has the cHTTPTransfer class that can be use to call RESTful services<\/p>\n<p>so in our test view we can simple use the cHTTPTransfer package and create an object based on the class as follows<\/p>\n<pre class=\"lang:default decode:true\">Use Windows.pkg\r\nUse DFClient.pkg\r\nUse cHttpTransfer.pkg\r\n\r\nDeferred_View Activate_oWebAPITest for ;\r\nObject oWebAPITest is a dbView\r\n\r\n    Set Border_Style to Border_Thick\r\n    Set Size to 200 300\r\n    Set Location to 2 2\r\n    Set Label to \"Web API Test\"\r\n    \r\n    Object oHTTPTransfer is a cHttpTransfer   \r\n        Set psRemoteHost to \"localhost\"\r\n        Set piRemotePort to 60387\r\n        Set pbShowErrorDialog to True\r\n    End_Object\r\n<\/pre>\n<p>We have to set the psRemoteHost property to the host name or ip address of the server the web service is running on. In our case we are using a local Visual Studio service so the psRemoteHost is localhost and the piRemotePort is 60387. The port also depends on your \u00a0service and could be 80 for the standard HTTP port or anything the service publisher uses to publish the service.<\/p>\n<p>The way the cHTTPTransfer class works is that it has an event called OnDataReceived that is called when data is returned from the server. So we have to implement this event<\/p>\n<pre class=\"lang:default decode:true\">    Object oHTTPTransfer is a cHttpTransfer   \r\n        Set psRemoteHost to \"localhost\"\r\n        Set piRemotePort to 60387\r\n        Set pbShowErrorDialog to True\r\n          \r\n        Procedure OnDataReceived String sContentType String sData\r\n            Showln sContentType \r\n            Showln \"-----------------------------------------------\"\r\n            Showln sData\r\n            Showln \"-----------------------------------------------\"\r\n            \r\n        End_Procedure\r\n    End_Object<\/pre>\n<p>we simply show the content type and the data<\/p>\n<p>Now lets see what it takes to call the GET verb to get all the customers<\/p>\n<p>Lets add a button and\u00a0add the following code to the OnClick procedure<\/p>\n<pre class=\"lang:default decode:true \">Procedure OnClick\r\n    Integer iRetVal\r\n    Get HttpGetRequest of oHTTPTransfer \"\/api\/customer\" to iRetVal\r\nEnd_Procedure<\/pre>\n<p>This will call the url \/api\/customer with GET request which in turn will then call the OnDataReceived event<\/p>\n<p>Here is the data we receive back<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapigetshowln.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-237\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapigetshowln.png\" alt=\"webapigetshowln\" width=\"479\" height=\"300\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapigetshowln.png 479w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapigetshowln-300x188.png 300w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapigetshowln-400x250.png 400w\" sizes=\"(max-width: 479px) 100vw, 479px\" \/><\/a><\/p>\n<p>the data type received is a JSON data type (application\/json)<\/p>\n<p>the data itself is as follows<\/p>\n<pre class=\"lang:default decode:true \">[\r\n  {\r\n     \"Id\":1,\r\n     \"Name\":\"StarZen Technologies, Inc\"\r\n  },\r\n  {\r\n     \"Id\":2,\r\n     \"Name\":\"Palms West Photo\"\r\n  }\r\n]\r\n<\/pre>\n<p>which is essentially a JSON array containing two customer records with each having an Id and a Name field<\/p>\n<p>All we have to do now is to parse the JSON data and use it in our application<\/p>\n<p>Now what if we wanted to save a new customer. This would require the use of a POST. In our simple example we used a standard web form with a POST request.<\/p>\n<p>We can do the same from DataFlex<\/p>\n<p>Lets add two forms and a button as follows<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipostscr.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-238\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipostscr.png\" alt=\"webapipostscr\" width=\"387\" height=\"320\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipostscr.png 387w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipostscr-300x248.png 300w\" sizes=\"(max-width: 387px) 100vw, 387px\" \/><\/a><\/p>\n<p>We also need to add some code to the OnClick procedure in the button of course<\/p>\n<pre class=\"lang:default decode:true\">        Procedure OnClick\r\n            Integer iRetVal\r\n            String sID sName\r\n\r\n            Get Value of oID to sID\r\n            Get Value of oName to sName\r\n            \r\n            String sData\r\n            Append sData (\"Id=\"+Trim(sID)+\"&amp;Name=\"+trim(sName)) \r\n            \r\n            Integer bOK\r\n            Send ClearHeaders to oHTTPTransfer \r\n            Get AddHeader of oHTTPTransfer \"Content-Type\" \"application\/x-www-form-urlencoded\" to bOK\r\n                        \r\n            Get HTTPPostRequest of oHTTPTransfer \"\/api\/customer\" sData False to iRetVal            \r\n        End_Procedure<\/pre>\n<p>Again very simple.<\/p>\n<p>We get the values from our forms. Then because we are passing the data as form url encoded data we create a string as follows<\/p>\n<p>Id=<em>identered<\/em>&amp;Name=<em>nameEntered<\/em><\/p>\n<p>Then we call ClearHeaders on the HTTP Transfer object to clear all the default headers.<\/p>\n<p>We then set the Content-Type header which is important as it will tell the web service what type of data we are sending<\/p>\n<p>In this case we are sending form url encoded data so the content type is set to<\/p>\n<pre class=\"lang:default decode:true\">\"application\/x-www-form-urlencoded\"<\/pre>\n<p>Then we are calling the HTTPPostRequest method passing the url and the data.<\/p>\n<p><a href=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipostresp.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-239\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipostresp.png\" alt=\"webapipostresp\" width=\"413\" height=\"338\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipostresp.png 413w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/webapipostresp-300x246.png 300w\" sizes=\"(max-width: 413px) 100vw, 413px\" \/><\/a><\/p>\n<p>as you can see the web service responded by sending back the new customer record<\/p>\n<p>if we click the GET\u00a0button again we receive the data set including the new customer we just created<\/p>\n<p>This post shows the basics of calling RESTful web services.<\/p>\n<p>In future posts we will dive deeper into the more detailed aspects.<\/p>\n\n\t\t<div class='author-shortcodes'>\n\t\t\t<div class='author-inner'>\n\t\t\t\t<div class='author-image'>\n\t\t\t<img src='http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/mike5crop-566174_60x60.jpg' alt='' \/>\n\t\t\t<div class='author-overlay'><\/div>\n\t\t<\/div> \n\t\t<div class='author-info'>\n\t\t\tMichael Salzlechner is the CEO of StarZen Technologies, Inc.<\/p>\n<p>He was part of the Windows Team at Data Access Worldwide that created the DataFlex for Windows Product before joining <a href=\"http:\/\/starzen.com\">StarZen Technologies<\/a>. StarZen Technologies provides consulting services as well as custom Application development and third party products specifically for DataFlex developers<\/p>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Want to call RESTful web services from your DataFlex Application? DataFlex doesnt support them out of the box but you can easily call them using this technique.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","ngg_post_thumbnail":0,"footnotes":""},"categories":[6,27],"tags":[],"class_list":["post-235","post","type-post","status-publish","format-standard","hentry","category-dataflex","category-dataflex-webapp"],"_links":{"self":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":3,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":270,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/235\/revisions\/270"}],"wp:attachment":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}