run cURL command from TCL script

Homepage Clovertech Forums Cloverleaf run cURL command from TCL script

  • Creator
    Topic
  • #111820
    Minli Yang
    Participant

    We are trying to post a report to Web API with security chanel (https).
    First step is to get a token from the web server then post the report contents with the token.

    I am trying to run cURL command from TCL script. When I tried on command line it works fine. I can get a token but when I run it from script, I have got an error as
    “ERROR:n couldn’t execute “curl -X POST https:/apps.viha.ca/auth/connect/token -H “Content-Type: application/x-www-form-urlencoded” -H “cache-control: no-cache” -d “grant_type=c”: no such file or directory”

    I have this code in tcl
    set cURLComand “curl -X POST https://……./token -H \”Content-Type: application/x-www-form-urlencoded\” -H \”cache-control: no-cache\” -d \”grant_type=client…&client_id=Clover&client_secret=clov%20id%20server%20sec\””

    if { [catch {exec $cURLComand} procStat] } {

    echo “ERROR:n $procStat”

    }
    and get this error below
    “ERROR:n couldn’t execute “curl -X POST https:/…/token -H “Content-Type: application/x-www-form-urlencoded” -H “cache-control: no-cache” -d “grant_type=c”: no such file or directory”

    I have done some research and someone suggested to exec command under a certain path from the enviroment path.

    with all the path below I could not tell which bin it should be in
    curl -X POST https://apps.viha.ca/auth/connect/token -H “Content-Type: application/x-www-form-urlencoded” -H “cache-control: no-cache” -d “grant_type=client_credentials&scope=EMR…..20secret”
    p= /cloverleaf/cis6.1/integrator/bin
    p= /cloverleaf/cis6.1/integrator/contrib
    p= /cloverleaf/cis6.1/integrator/sbin
    p= /cloverleaf/cis6.1/integrator/dbms/bin
    p= /cloverleaf/cis6.1/integrator/tcl/bin
    p= /cloverleaf/cis6.1/integrator/clgui/bin
    p= /cloverleaf/cis6.1/integrator/clgui/java/bin
    p= /cloverleaf/cis6.1/integrator/usercmds
    p= /usr/bin
    p= /etc
    p= /usr/sbin
    p= /usr/ucb
    p= /home/hci/bin
    p= /usr/bin/X11
    p= /sbin
    p= .
    p= /usr/local/bin
    p= /usr/local/scripts
    p= /bin
    p= /home/hci/bin
    p= /usr/bin/X11
    p= /usr/openwin/bin
    p= /usr/bin/X11
    p= /usr/openwin/bin
    “ERROR:n couldn’t execute “curl -X POST https:/…token -H “Content-Type: application/x-www-form-urlencoded” -H “cache-control: no-cache” -d “grant_type=c”: no such file or directory”

    Please help and I am not sure if I am in the right track on this.
    with the work we are trying to do, it is better to use tclcURL or cURL command line here.

Viewing 5 reply threads
  • Author
    Replies
    • #111824
      Charlie Bursell
      Participant

      You need to provide full path to cURL.  The script is running as a child process and does not inherit all the paths.

      Is there a reason you do not use TclCurl?

    • #111857
      Minli Yang
      Participant

      Thank you Charlie for your help. I got a permission denied error after provide the path.  I am not sure if it is in correct syntax…

      I have tried use the tclcURL and it is working on this part.

      The second portion  is to send a report with a file path  and the token I retrieved. It is new to me and trying to find the correct syntax. If you have something similar and be able to share, that would be great.

      Thanks and appreciate your time.

    • #111862
      Eric Fortenberry
      Participant

      I believe the problem is that exec expects a list of arguments, not a single string:

      Incorrect Example: exec “ls -ltr”

      Correct Example: exec ls -ltr

    • #111867
      Charlie Bursell
      Participant

      Eric makes a valid point about exec but no need to exec cURL when TclCurl is available.

      I would have to get more involved with the specifics of what you are trying to do to offer any further help.  Perhaps a few Implementation hours from Infor?

    • #112127
      Dustin Sayes
      Participant

      if i had to guess, your url will change with each message. so build the url variable…

      set url $urlConstant/$msgID/file  # or something similar. You are doing an http get, so the webserver will need specifics in the URL. Once you post you’ll get an http status code, not the token you are hoping for. Any sort of data back will be handled outside of handing the http post action.

      now set the curl command, with all the required switches.

      $curlHandle configure -url $url \
      -userpwd $userid:$sample_pwd \
      -file $rcvFile \
      -httpget 1 \
      -httpversion 1.1 \
      -headervar httpHeader \
      -verbose 1

      catch ($curlHandle perform) curlExitCode #this will execute the command and catch the http header that is returned.  now split the header to get the http code.. .now you can have more informaiton on whta the server is replying with..

      set httpStatCode [lindex [split $httpHeader(http) ” “] 1] #or something like that

       

      this is a manual way to accomplish your task, and i’m certain you could program the whole thing, but cloverleaf does have some built in tools to make this a better process. i think it is the java ws server protocol, in the netconfig, then you should be able to watch the process log for the http server response… but you can use the commands above to at least confirm the http server response.

      • This reply was modified 4 years, 10 months ago by Dustin Sayes.
    • #112367
      Rob Lindsey
      Participant

      I have done something like this where our system has to call a Web Service to get a token and then if a good token is gotten, send the data message with the token received.

      Logic to get the token using a TclCurl call

      <code>

      set curlHandle [curl::init]
      if { $debug } {
      puts “Command ID ==>> $curlHandle”
      puts “The Configs are: URL==> $URL”
      puts “Https Headers ==> $httpHeaders”
      }

      set reply “”
      catch {
      curl::transfer -verbose 1 -url $URL -header 1 -httpheader $httpHeaders -post 1 -postfields $soapMsg -bodyvar reply -errorbuffer err -sslverifypeer 0
      } tclerr
      if { $debug } {
      puts “tclerr ==> $tclerr”
      puts “reply ==> $reply”
      }

      $curlHandle cleanup

      </code>

      The logic gets the token out of the reply variable and then uses it in the next TclCurl call.

      <code>

      set curlHandle [curl::init]

      if { $debug } {
      puts “Command ID ==>> $curlHandle”
      puts “The Configs are: URL==> $URL”
      puts “Https Headers ==> $httpHeaders”
      }

      set reply “”
      catch {
      curl::transfer -verbose 1 -url $URL -header 1 -httpheader $httpHeaders -post 1 -postfields $soapMsg -bodyvar reply -errorbuffer err -sslverifypeer 0
      } tclerr
      if { $debug } {
      puts “tclerr ==> $tclerr”
      puts “reply ==> $reply”
      }

      $curlHandle cleanup

      </code>

       

      Rob

Viewing 5 reply threads
  • You must be logged in to reply to this topic.

Forum Statistics

Registered Users
5,105
Forums
28
Topics
9,278
Replies
34,382
Topic Tags
281