Rocket U2 | UniVerse & UniData

Expand all | Collapse all

Executing a API curl command from UniBasic

  • 1.  Executing a API curl command from UniBasic

    Posted 09-13-2021 13:39

    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
    ------------------------------


  • 2.  RE: Executing a API curl command from UniBasic

    Posted 09-13-2021 13:48
    Hi Peter,

    The execute or perform statement will execute what is possible at the Unidata command line.  In Universe pick i would need to execute the following

    On Windows
    STMT = "DOS -c 'curl -o out.json www.example.com/index.html'"
    PCPERFORM STMT

    and Linux

    STMT = "SH -c 'curl -o out.json www.example.com/index.html'"
    PCPERFORM STMT


    ------------------------------
    greg Livingston
    Universe Practice Lead
    OUTsurance Insurance
    Centurion GA South Africa
    ------------------------------



  • 3.  RE: Executing a API curl command from UniBasic

    ROCKETEER
    Posted 09-13-2021 15:26
    Edited by Michael Belme 09-13-2021 15:27
    Hi Peter,

    I gave your program a test on UniData 8.2.3 on Windows. It seems to work and the out.json file will be created at the OS level in the account you run the program from:

    :CT BP CURLTEST
    BP:

    CURLTEST
    STMT = "curl -o out.json http://www.example.com/index.html"
    PCPERFORM STMT
    END:
    :RUN BP CURLTEST
    % 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 39250
    :

    So this should work. My only programming difference is the final 'END' last line. Do you get an error message or just no response? I started with 'curl -V' as the command to make sure I was getting output back first via PCPERFORM before moving on to the request.

    We also have the callHTTP extension within UniBASIC if you'd like to run the HTTP request directly in BASIC without the external curl call.

    Feel free to open a support case with Rocket if you'd like to discuss further.

    Thanks,
    Michael Belme
    Rocket U2 Support

    ------------------------------
    Michael Belme
    Technical Support
    Rocket Internal - All Brands
    DENVER CO United States
    ------------------------------



  • 4.  RE: Executing a API curl command from UniBasic

    PARTNER
    Posted 09-16-2021 08:32
    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
    ------------------------------



  • 5.  RE: Executing a API curl command from UniBasic

    Posted 09-15-2021 10:53
    Thank you Greg.  I was hoping to execute the curl command from the client (pc) not the server.   Our HPUX doesn't have curl.

    -Peter G

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



  • 6.  RE: Executing a API curl command from UniBasic

    Posted 09-15-2021 11:08
    Michael, 
    Is you curl command being execute on the Unidata Windows server?  I'm trying to execute the curl command on the client side and capture the data there.   Would Integrate's WIN.EXPORT WIN.IMPORT do what I'm looking for?

    Our version of Unidata HPUX is very old and it does not have the "call..." extensions.   


    Thanks,
    -Peter G

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



  • 7.  RE: Executing a API curl command from UniBasic

    Posted 09-14-2021 09:11
    Edited by Nik Kesic 09-14-2021 09:16
    Peter,

    This sounds like to me you are having an issue at the HPUX platform.

    Have you tried to run your curl command from the HP UNIX shell?

    What you could do from your telnet session is:

    At the ECL prompt(:) type:

    !curl -o out.json Example Domainwww.example.com/index.html

    The ! (exclamation)  will send you to the UNIX shell and execute the command following the !


    ------------------------------
    Nik Kesic
    DevOps
    LKQ
    NASHVILLE TN United States
    ------------------------------



  • 8.  RE: Executing a API curl command from UniBasic

    Posted 09-15-2021 11:14
    Nik,
    Our HPUX server does not have curl.  maybe I should install curl in the HPUX. I want the curl command to execute at the client level not the server.   Thanks. 

    -Peter G

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



  • 9.  RE: Executing a API curl command from UniBasic

    ROCKETEER
    Posted 09-15-2021 13:13
    Hi Peter,

    Oh I see now... the way you were programming it would have been to run on the server side. In this case to run it on the client side and have it initiated by the host you will definitely have wIntegrate as part of the picture.

    Let me spend some time looking into this today and see what I can come up with.

    Regards,
    Michael

    ------------------------------
    Michael Belme
    Technical Support
    Rocket Internal - All Brands
    DENVER CO United States
    ------------------------------



  • 10.  RE: Executing a API curl command from UniBasic

    ROCKETEER
    Posted 09-15-2021 14:28
    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
    ------------------------------



  • 11.  RE: Executing a API curl command from UniBasic

    Posted 09-15-2021 11:20
    Hello Peter,

    I dont have the wIntegrate manual, but I know it is a powerful product with a lot of functions. I am sure Michael Belme can help you figure out which win.prog would trigger a pc event from basic.


    ------------------------------
    Nik Kesic
    DevOps
    LKQ
    NASHVILLE TN United States
    ------------------------------



  • 12.  RE: Executing a API curl command from UniBasic

    Posted 09-15-2021 16:34
    Michael,
    This is great! That is thinking outside the box. Thank you very much.

    So,
    1. Build the curlcommand.bat file with UniBasic.
    2. Use WIN.PCWRITE curlcommand.bat file on the client's pc drive.
    3. Use WIN.PCRUN2 to execute curlcommand.bat.
    4. Use WIN.PCREAD to get the output back into Unidata.

    I said WIN.PCWRITE and WIN.PCREAD just to simplify the logic, there will be more logic added.  I'll post the program when done.

    -Peter G



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



  • 13.  RE: Executing a API curl command from UniBasic

    Posted 27 days ago
    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
    ------------------------------