CMD/Bash Script Ninja - cURL Response Header Number Manipulation

Posted at

Use cURL response headers using either --head or --include (to include the headers with the response-body),

For the following example we will use GREP, SED (gnu can be compiled either Win32 or Linux, heck! even use cygwin in Windows [as long you put the c:\\cygwin\\bin\\ in PATH, it is super easy])
and Content-Length response-header,

- no "on screen" output (perfect for in-scripts)
- no temporary-file saving (R/O I/O are considered bad practice.. and slow..)
- working out PIPE action (suitable for both Windows and Linux)
- processing and simple arithmetics on value (no just a "stupid" string..)

@echo off

setlocal enableextensions
for /f "tokens=*" %%a in ('curl --head --ipv4 --sslv3 --silent --location-trusted --url "https://storage.googleapis.com/chromium-browser-continuous/Win_x64/362418/mini_installer.exe" 2^>^&1 ^| grep "Content-Length" 2^>^&1 ^| sed "s/\\
$//g"
2^>^&1 ^| sed "s/content-length\\:\\ //gi" 2^>^&1') do ( set /a num=%%a )

set /a num=%num% / 1024 / 1024

echo it is %num%MB
endlocal


will output
it is 41MB


(original header was Content-Length: 43597312)


(naturally a pure bash variable output is simpler.. but interior of pipe processing is still -kind'a- similar, and thus- useful or at least quite interesting..)

Hope it will be useful I've saw no StackOverflow nor any Google-related resources providing anything similar to that..