Skip to main content
Peter Gonzalez
Participating Frequently
Sep 13, 2021 12:53 PM UTC

Executing a API curl command from UniBasic

Hello group!

I should know this but it's not working for me. I'm trying to execute a API curl command from my Unidata Unibasic flavor running HP/UX system. The emulator is wIntegrate but I don't think that matters.  What is the correct syntax to execute an API Curl command from my PC?   Thanks!

*** This works  from command prompt ***
C:\\Users\\gonzalp>curl -o out.json http://www.example.com/index.html
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1256 100 1256 0 0 1256 0 0:00:01 --:--:-- 0:00:01 26723


*** This does not work from UniBasic ***
 STMT = 'curl -o out.json http://www.example.com/index.html'
PCPERFORM STMT



------------------------------
Peter Gonzalez
programmer
Self Registered
University City MO United States
------------------------------

    12 replies

    Manu Fernandes
    Inspiring
    Sep 16, 2021 8:32 AM UTC

    Hello group!

    I should know this but it's not working for me. I'm trying to execute a API curl command from my Unidata Unibasic flavor running HP/UX system. The emulator is wIntegrate but I don't think that matters.  What is the correct syntax to execute an API Curl command from my PC?   Thanks!

    *** This works  from command prompt ***
    C:\\Users\\gonzalp>curl -o out.json http://www.example.com/index.html
    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    100 1256 100 1256 0 0 1256 0 0:00:01 --:--:-- 0:00:01 26723


    *** This does not work from UniBasic ***
     STMT = 'curl -o out.json http://www.example.com/index.html'
    PCPERFORM STMT



    ------------------------------
    Peter Gonzalez
    programmer
    Self Registered
    University City MO United States
    ------------------------------
    Hi Peter,

    I know you have the solution with wintegrate.
    One another option is using the 'CallHTTP' toolset from uniBASIC and then you do not need to deploy curl and you are not client dependant. 
    Have a look into the documentation  UniData/UniBasicExtensionsRefGuide, chapter 2 - using call HTTP

    I hope this help

    ------------------------------
    Manu Fernandes
    ------------------------------
    Peter Gonzalez
    Participating Frequently
    Sep 27, 2021 12:10 PM UTC
    Hi Peter,

    I spent some time working on this and came up with something that works. I'm not quite sure how you want this setup to run on the client PC, so just went with what works first.

    The BASIC program using the wIntegrate host routines looks like this:

    CALL WIN.PCRUN2("C:\\users\\mbelme\\desktop\\curlcommand.bat","",0, RESP)
    IF RESP < 32 THEN
    PRINT "Failed to run curl, error code: ":RESP
    END ELSE PRINT "curl command started"

    So when you run this it will launch a Windows batch script on the client PC located on my user's desktop called curlcommand.bat. That batch script is setup like this inside:

    @ECHO OFF
    echo Running curl command:
    echo.
    curl.exe -o c:\\users\\mbelme\\desktop\\out.json http://www.example.com/index.html
    echo.
    cmd.exe /k

    It will open a Windows command prompt and run the command on the client moving focus to that window and off wIntegrate. Obviously you can modify that however you want it setup. The last line of 'cmd.exe /k' leaves the command prompt window open. Get rid of that line if you want it to just self-close once it runs. Refer to the wIntegrate Host Subroutines Reference for more info on WIN.PCRUN2.

    I tried passing the curl.exe command direct into WIN.PCRUN2, but I couldn't find any combo to get it to run and successfully create that output file. So this was the combo I came up with to get it to run properly. Let me know if you have questions.

    Thanks,
    Michael

    ------------------------------
    Michael Belme
    Technical Support
    Rocket Internal - All Brands
    DENVER CO United States
    ------------------------------
    Michael and others,
    Below is the UniBasic program using wIntegrate calls that will execute a Curl command and retrieve a json file from a website.  The results.json is placed in a newly created Pie_Downloads directory. Note: directories can't have a blank space in the name.  From this point on you can access the results.json file and do as you want locally on the client. 

    -Peter G  


    PATHNAME = "C:\\Pie_Downloads"
    FILENAME = "CURLCOMMAND.BAT"

    STMT = ""
    STMT<-1> = "@ECHO OFF"
    STMT<-1> = "echo Running curl command:"
    STMT<-1> = "echo."
    STMT<-1> = "curl.exe -o ":PATHNAME:"\\results.json http://www.example.com/index.html"
    STMT<-1> = "echo."
    STMT<-1> = "cmd.exe /k"
    *
    * Verify that the Download folder is there.
    *
    CALL WIN.PCDIR(PATHNAME, FOUND)
    IF NOT(FOUND) THEN
    *
    * Create The "Download Folder" If Not Found.
    *
    CALL WIN.PCMKDIR(PATHNAME)
    END
    *
    * Opens The File Or Creates The File.
    *
    CALL WIN.PCOPEN("CURL", PATHNAME:"\\":FILENAME, FOUND)
    IF NOT(FOUND) THEN
    *
    * Create The "CURLCOMMAND.BAT" If Not Found.
    *
    CALL WIN.PCCREATE(PATHNAME:"\\":FILENAME)
    *
    * Opens The File Or Creates The File.
    *
    CALL WIN.PCOPEN("CURL", PATHNAME:"\\":FILENAME, FOUND)
    END
    *
    * Writes The Contents To The File.
    *
    CALL WIN.PCWRITE("CURL", STMT, "TEXT") ;* "TEXT" Swaps a crlf
    *
    * Closes The File. - Very Important
    *
    CALL WIN.PCCLOSE("CURL")
    *
    * Execute The Bat Program.
    *
    CALL WIN.PCRUN2(PATHNAME:"\\":FILENAME, "", 0, RESP)

    STOP

    ------------------------------
    Peter Gonzalez
    programmer
    Self Registered
    University City MO United States
    ------------------------------