Jump to content

Recommended Posts

Posted (edited)

I've been trying to capture the output of the nccs.php file using Curl, but I cannot get it to work correctly. First here's my Autoit code. (Note: I have double checked that $sDate is correctly formatted):

;Set path to Curl
    $sPath = EnvGet ('PATH')
    $sPath = $sPath & ';C:\utility\Curl\bin\'
    EnvSet ('PATH', $sPath)

; Run Curl
    $cmd = 'curl.exe -u username:password https://instanthousecall.com/x/admin/nccs.php?startDate=$sDate$'
    $iPID = Run('$cmd$', "C:\utility\Curl\bin\", @SW_HIDE, $STDOUT_CHILD)
    ProcessWaitClose($iPID)
    $sHTML = StdoutRead($iPID)
    MsgBox (262144, '', $sHTML)

Here's the display of MsgBox  showing the @sHTML varaible on the last line:

MessageBox.png.6fb38a0b11cdd543bc2863be6fd52a94.png

 

Now if I open a Command Window and change to the directory where Curl.exe is, and then run the exact same command line this is what I get:

CommandLine.thumb.png.6d75767b2dbee97c0b5e2bb2ce9ca4c9.png

Note the text after "/head" in the  command window. That is the text that I need to see in the Autoit script.

I feel like I'm very close, but I can't figure out why it only works in the command window.

Any help would be greatly appreciated!

Thanks, John

 

Edited by major4579
Posted

From the help file --

Quote

StdoutRead() does not block, it will return immediately. In order to get all data, it must be called in a loop.

Have you tried reading the output in a loop? --

Local $sOutput = ""
        While 1
                $sOutput &= StdoutRead($iPID)
                If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
                        ExitLoop
                EndIf
        WEnd
        
        MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)

 

Posted

Hi @major4579,

in case the suggested way of @Danp2 doesn't work for you, you could also work-around it by transfer the cURL response to a file and then read this [...] proceed it. I know this would not very elegant, but it should work at least.

Something like this:

$cmd      = 'curl.exe -u username:password https://instanthousecall.com/x/admin/nccs.php?startDate=$sDate$'
$filePath = @ScriptDir & '\response.txt'
$iPID     = Run('$cmd$ > "' & $filePath & '"', "C:\utility\Curl\bin\", @SW_HIDE, $STDOUT_CHILD)

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted

@SOLVE-SMART

It's doing the same thing.

If I run

C:\utility\Curl\bin\curl.exe -u username:passwordhttps://instanthousecall.com/x/admin/nccs.php?startDate=2023-01-11 > D:\IHC.txt

In a command window (does not matter what location I'm running it from) i works fine D:\IHC.txt contains what I need.

If I run the exact same line in Autoit, D:\IHC.txt never even gets created.

$cmd = 'C:\utility\Curl\bin\curl.exe -u username:password https://instanthousecall.com/x/admin/nccs.php?startDate=2023-01-11 > D:\IHC.txt'

$iPID = Run('$cmd$', 'C:\utility\Curl\bin', @SW_HIDE)

But if I run 

$cmd = 'C:\utility\Curl\bin\curl.exe -u username:password https://instanthousecall.com/x/admin/nccs.php?startDate=2023-01-11 > D:\IHC.txt'

$iPID = Run($cmd, 'C:\utility\Curl\bin', @SW_HIDE)

It does work. For what it's worth I do have "Opt ("ExpandVarStrings", 1)  " at the beginning of my autoit file.

Posted

Finally, This code works:

$cmd = 'C:\utility\Curl\bin\curl.exe -u username:password https://instanthousecall.com/x/admin/nccs.php?startDate=' & $sDate

$iPID = Run($cmd, 'C:\utility\Curl\bin', @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($iPID)
$sHTML = StdoutRead($iPID)

Previously in the $cmd line I had $sDate in the end of the string as $sDate$ and in the Run command I had '$cmd$'. But for some reason I haven't figured out the Opt ("ExpandVarStrings", 1)  had stopped working . Interestingly it works everywhere else as far as I can see. In fact if I add MsgBox (262144, '', '>>>$sHTML$<<<') after the last line above that works correctly!

Go figure.

Thanks to both Danp2 and SOLVE-SMART for sticking with me while we figured this out.

BTW, who should get the credit?

John

Posted (edited)
30 minutes ago, major4579 said:

Finally, This code works [...]

Nice 😀 .

 

7 minutes ago, Danp2 said:

[...] You should give credit to @mikellsince using curl was his idea.

Agreed 👍 .

A last tryout could be the usage of ShellExecute() instead of Run(). Just to ensure the bahavior is the same or not.

Best regards
Sven

Edited by SOLVE-SMART

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted

@SOLVE-SMART

I tried ShellExecute with the output of curl directed to a file (since ShellExecute doesn't support $STDOUT_CHILD) and it didn't work any better.

@Danp2

I did try the loop after the ProcessWaitClose and it still didn't work.

Thanks again to both of you!

I give @mikell the credit.

-John

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...