IS.HTTPCLIENT - Using CURL and BASIC's EXECUTE

CURL is a O/S level program that can be found for just about every operation systems out there. CURL is a command-line tool that provides users and developers the ability to transfer or request data with http URLs.

CURL is an open source tools and can be found on every version of Linux being distributed. CURL can be find as compiled binaries as well as compliable source code. Many software applications integrate CURL using the libcurl, but in this case we will be using the curl command line routine using the command-line switches, standard-in, and standard-out to transmit information between MultiValue BASIC and CURL.

You can find out more about CURL at:

http://curl.haxx.se

Since we have to setup, send and receive information through O/S commands, there are serveral hoops you have to jump through to access CURL. While not hard, it could take a little trial and error to make it work.

The subroutine included with this article removes that set of complexity by creating a single subroutine that you can call.

SUBROUTINE IS.HTTPCLIENT(HTTP.METHOD,URL,HEADER.DATA,POST.DATA,RESP.HTTP.STATUS,RESP.HEADERS,RESP.DATA,ERROR)
  • HTTP.METHOD
    This is the HTTP Method you are asking the web server to process. Normally, this is either 'GET' or 'POST'
  • URL
    This is the "Http://" or "Https://" want to request data from or send data to.
  • HEADER.DATA<1,n>
    Http Request Header Name - This is the header name that the developer wants to included with the request to the server.
  • HEADER.DATA<2,n>
    Http request Header data - This is the data you want to include with this header name
  • POST.DATA<-1>
    This is the data you want to send to the web server. Each AM (Char(254)) will be converted to a CRLF.
  • RESP.HTTP.STATUS
    Http response returned by the web server. That values are between 100-500. Most successful request will return with a value of 200 or 201 as per the HTTP protocol.
  • RESP.HEADER<1,N>
    Header Names returned with the response data. This is provided by the web server, and may include important information.
  • RESP.HEADER<2,N>
    Value for the header data
  • RESP.DATA<n>
    This is the data returned by the web server based on the URL, HTTP.METHOD, and POST.DATA. Each CRLF will be converted into an AM mark (char(254))
  • ERROR
    All other communication errors, parsing error, or anything not related to the HTTP protocol will be returned here. If there is no error, then this will return '' or '0'

Example:

CALL IS.HTTPCLIENT("GET","http://www.google.com","","",HTTP.STATUS,"",HTTP.RESP,ERROR)
IF (ERROR<1> GT 0) THEN
  CRT "Error ": ERROR
  STOP
END
*
CRT HTTP.RESP[1,200]

EXECUTE O/S commands

Depending on which database and which operation system your database is working on, this syntax is slightly different. There are associated articles that talk about how to access Standard-In and Standard-Out using MultiValue BASIC EXCEUTE statement. Please review the related articles for details.

This subroutine will use another subroutine called IS.EXECUTE.OS:

SUBROUTINE  IS.EXECUTE.OS(CURL.CMD,CURL.STDIN,CURL.STDOUT,EXIT.CODE)

Since the CURL command line can be a little picky, there are a few other programs that are OS Specific as well:

SUBROUTINE IS.WRITE.OS(OS.PATH,DATA.ITEM,OS.FILENAME,ERROR)
SUBROUTINE IS.DELETE.OS(OS.PATH,OS.FILENAME,ERROR)

These programs are supplied in related articles as well. Make sure you pull the correct version of the programs.

Security Note:

A temporary file is written the O/S into a temp folder in order to handle Standard-in correctly. While short-live, and promptly deleted, it may trigger security concerns. Be sure to encrypt the temp folder.

CURL switches used:

CURL has many options and can do more than http request. Because of this, there are a few switches that are important.

-k = Fixes the 'SSL certificate problem, verify that the CA cert is OK' error message. This happens when working with websites using newer SSL certificates.

-s = Suppresses a UI meter. While this is nice to have when working with the command-lines, we don't want to deal with it when capturing the output from EXECUTE.

-i = Included the headers when display the response from the web server. If this is not included, then only the data is returned in standard-out, and you don't get any response header data.

-L = (upper case is important). Follow any redirects. This will force the CURL to follow temporary and permeate redirects until it gets the actual data. Not required, but helpful.

CURL setup

Since CURL can be found in different places within in your O/S, this program will look for a record called 'CURL.CONFIG' in the MD or VOC of the account. This allows you to configure the CURL command-line setup without changing the program.

CURL.CONFIG

<1> = 'CONFIG'

<2> = Name and/or path of the executable. On Linux, this is usually 'curl'. For windows, it should be '"c:\Program File\curl\curl.exe"'. Windows installs of curl may be in different places, so make sure you have the full page included.

<3> = All the switches to include on the command line. Leave blank, and the above switches will automatically be included.

License Notes:

This program is free software in the public domain; you can redistribute it and/or modify it in any way you wish. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Commercial support and warranties of this software and code can be purchased through International Spectrum.

This program is not the only means to do this, but is a starting point or working sample that can be extended.

Downloads:

International Spectrum

Located in Thorton CO.

View more articles

Featured:

IS.HTTPCLIENT - Using UniVerse and UniData UniBASIC Sockets

Read and Writing Unix/Linux Files from UniVerse/UniData

Read and Writing Windows Files from UniVerse/UniData

Read and Writing Windows files from D3

Read and Writing Unix/Linux Files from D3

menu
menu