2

I have a loop in wich I check urls from file one-by-one and want to get the status header and the execution time of every request as result. I have two curl commands:

This one outputs the header communication (not wanted) and at the end the http status code and time 200 - 0,016

curl -w "%{http_code} - %{time_connect}" -I -s http://superuser.com >> test_result.txt

This one gets the line with the http status code from the header and prints it to the file HTTP/1.1 200 OK

curl -v -I -s http://superuser.com | findstr /c:"HTTP" >> test_result.txt

How can I combine these two commands to get output from one request as this

HTTP/1.1 200 OK - 0,016

so extracting the line with http header and appending the execution time without all other headers in file

1 Answer 1

3

You can use a batch file

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Just to test - Generate a file with URLs
    > ".\urls.txt" (
        echo http://superuser.com
        echo http://www.google.com
    )

    > ".\testResults.txt" (
        for /f "useback delims=" %%u in (".\urls.txt") do (
            set "statusCode="
            echo([%%u]
            for /f "tokens=1,2 delims=#" %%a in ('
                curl -w "##%%{time_connect}##." -I -s --url "%%~u"
                ^| findstr /l /b /c:"HTTP/" /c:"##"
            ') do if "%%b"=="." (
                setlocal enabledelayedexpansion
                echo(    !statusCode! - %%a
                endlocal
            ) else (
                set "statusCode=%%a"
            )
        )
    )

Just two nested loops. The first iterates over the URLs file and the second executes and processes the output of the curl command.

The output of curl command is filtered with findstr to retrieve only the lines with the status code and the time to connect (the output string in -w has been modified to locate this data in the output)

4
  • Nice. Not being on Windows (and my Windows VM not having curl available), before upvoting: can I boldly assume this creates lines like HTTP/1.1 200 OK - 0,016? (Heck, nice anyway.)
    – Arjan
    Commented May 22, 2015 at 8:45
  • @Arjan, yes, and an aditional previous line with the url in square brackets (the previous echo([%%u]) just included to see from where curl is fetching the information.
    – MC ND
    Commented May 22, 2015 at 8:54
  • yeah it works! thank you, could you please provide one command that could be used directly from cmd as an addition? with some example url
    – Zavael
    Commented May 25, 2015 at 7:23
  • -w '%{http_code}' in curl call should return the HTTP status code
    – Fr0zenFyr
    Commented Jul 16, 2019 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .