Friday, June 24, 2016

CURL for Win64: Download, Installing and using CURL to test RESTful Web Services

This posting demonstrates how to acquire, set up and run CURL, a command-line utility that allows RESTful web services to be tested and exercised. The target platform for this demonstration will be CURL running on 64-bit Windows.

Background

Over the years I have developed RESTful web services that were consumed by different client platforms. As a .NET developer it is tempting to use C# or JavaScript to test and use as examples (an SDK) of how to consume a web service. To be candid it might not do a Python or Java developer a lot of good to show them a C# sample of how to consume a web service.

Instead of relying on languages supported by .NET, I have used third party utilities to demonstrate and test web services.. There are many excellent candidates to choose from with respect to these utilities: CURL, Postman, SoapUI, TestMaker, and WebInject. I typically use the HTTP client tool used by majority of the developers I work with. When given a choice or when I am providing examples to other developers, I use CURL. The CURL utility is command-line based so examples can be handed out as sample scripts (CURL command plus command-line options) to be run. Any developer can reverse engineer CURL into their own client development language (C#, JavaScript, Pytho, Java,, C++ etc.). A Windows-based CURL example can be run on a Linux system by simply changing the double quotes to single quotes.

FYI: CURL is more powerful and does far more than invoke RESTful services. The CURL utility is general purpose HTTP client but this is beyond the content of this post.

Prerequisites 

Before downloading CURL, make sure you know if you Windows 32-bit or 64-bit. If you are unsure how to do this please see, "Are you running 32-bit Windows or 64-bit Windows?"

Additionally, download and install 7-Zip. The compression utility,7-Zip, is superior to the zip format provided natively with Windows because it provides a tighter level of compression. 7-Zip can be downloaded from  "http://www.7-zip.org/download.html." The CURL download is typically compressed in 7-zip format hence the 7-zip compression utility is required.

Downloading Curl

The "https://curl.haxx.se/dlwiz/" link displays a wizard allowing a user to choose the correct version of CURL. Running the aforementioned link in a browser displays the following:


In the previous screen under the heading, "Select Type of Package," select the "curl executable" link. Clicking on the curl executable link displays the following:


Under "Select Operating System," click on the drop down containing  button which displays the following:


Select "Win64" from the dropdown and click on Select! button. This action display the following web page:


Under "Select for What Flavour" leave the setting as "Generic" and click on the Select! button:



Under "Select which Win64 Version" leave "Any" select click on the Select! button:


Under "Select for What CPU" select x86_64 from the drop down and click on Select!. This reveals the version of CURL that the wizard recomends for download:


The top link is located in U.K. and when clicked on leads to http://winampplugins.co.uk/curl/:


For a 64-bit version of Windows download the 64-bit version of CRUL(on the previous screen version 7.49.1).

Setting Up CURL

I a using the terminology "setting up CURL" and not "Installing CURL." The CURL utilities provide for download do not come with an installer. For this reason first create a location where to copy the CURL folder to. My location for third-party applications that do not come with an installer is C:\bin, hence this is the location to which CURL will be set up.

Since 7Zip is installed right clicking on the CURL download (extension .7z) will contain a 7-zip menu and an option to "Extra to" a folder with the same as the CURL download (see below where CURL is extracted to curl_7_49_1_openssl_nghttp2_x64):


On my machine I take the <downloads>\curl_7_49_1_openssl_nghttp2_x64 folder and copy it to my C:\bin folder. The contents of curl_7_49_1_openssl_nghttp2_x64 are as follows:

To make life simpler let's add the C:\Bin\curl_7_49_1_openssl_nghttp2_x64 to our system path. This is handled by navigating to Control Panel:


Click on System and Security which displays the following:


Click on the System folder within System and Security:


On the left side click on Advanced system settings:


Click on Environment Variables:


Under System variables, select the Path system variable and click on the Edit button:


Windows 10 elegantly shows each path associated with with the Path environment variable on a separate line (thank you Microsoft; it is about time). Click on the New button so the path to CURL can be added:


Paste the path where the CURL.exe is located into the area created by the New button (for my machine this is C:\Bin\curl_7_49_1_openssl_nghttp2_x64).


Once the path to CURL has been added, click on OK. After this click on OK two more times to close on the windows opened to add the path to CURL to the Path environment variable.

Testing CURL

The best way to insurece CURL is working is to test it against working web services. These are a set of free web services documented at:

http://www.groupkt.com/post/f2129b88/free-restful-web-services-to-consume-and-test.htm

One of the RESTful services provided is found at http://services.groupkt.com/country/get/all. This UIR was added to a Windows batch file which is follows:

REM REST web-service to get a list of all Countries
REM http://services.groupkt.com/country/get/all
REM 
REM This rest web service will return a list countries in JSON 
REM format, each country 
REM object has 
REM {
REM "name":"India",
REM "alpha2_code":"IN",
REM "alpha3_code":"IND"
REM }
REM etc.

CURL "http://services.groupkt.com/country/get/all"

The reason the URI is wrapped in double quotes is to make sure that all any characters in the URI that are invalid in a Windows command-line do not cause execution of CURL to generate an error. When the previous code is executed as a batch file it displays the following (demonstrating CURL works):


No comments :

Post a Comment