How do I pipe or redirect the output of curl -v?
add the -s
(silent) option to remove the progress meter, then redirect stderr to stdout to get verbose output on the same fd as the response body
curl -vs google.com 2>&1 | less
Getting a piped curl command output to a variable in bash
You need to remove the -o option that writes the output to a file, in your case /dev/null. Here is your updated code that should work.
$ response=$(curl -k -x $i:1234 -U $u:$p -w $outputformat -s $site | cut -d ' ' -f 2)
Another way to check is to remove pipe and send your STDERR to /dev/null. This will show you what is getting passed to your cut command.
You should see something if all is happy.
$ curl -k -x $i:1234 -U $u:$p -s $site 2>/dev/null
If you don't get any output, then remove the STDERR redirect and echo the return code.
$ curl -k -x $i:1234 -U $u:$p -s $site
$ echo $?
0
If the exit code is anything other then 0 you have an error. You can find the error codes in the man page or this site has pretty good write up.
No to get the response code.
$ curl -w "HTTP Response: %{http_code} - URL: %{redirect_url}\n" -o /dev/null -s http://yahoo.com/
HTTP Response: 301 - URL: https://yahoo.com/
$ curl -w "%{http_code}\n" -o /dev/null -s http://yahoo.com/
301
$ response=$(curl -w "%{http_code}\n" -o /dev/null -s http://yahoo.com/)
$ echo $response
301
$ curl -w "%{http_code}\n" -o /dev/null -s http://yahoo.com/ | cut -d' ' -f3
301
$ response=$( curl -w "%{http_code}\n" -o /dev/null -s http://yahoo.com/ | cut -d' ' -f3)
$ echo $response
301
Redirect curl to while loop
You will want to redirect the output of curl using process substitution as follows:
while read -r l; do
echo "123 $l"
done < <(curl 'URL')
You can also use the output of a quoted command substitution and a herestring as follows:
while read -r l; do
echo "123 $l"
done <<<"$(curl 'URL')"
(though process substitution is preferred)
note: for redirecting to a file, you can redirect the output of a block, rather than a line at a time:
:>outfile ## truncate outfile if it exists
{
while read -r l; do
echo "123 $l"
done < <(curl 'URL')
}>outfile
Curl with pipe : How to print the error without the progress-meter?
The flag you are looking for is -sS
. From man curl
:
-s, --silent
Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute. It will still output the data you ask for, potentially even to the terminal/stdout
unless you redirect it.
Use -S, --show-error in addition to this option to disable progress meter but still show error messages.
See also -v, --verbose and --stderr.
Curl to return http status code along with the response
I was able to get a solution by looking at the curl doc which specifies to use -
for the output to get the output to stdout.
curl -o - -I http://localhost
To get the response with just the http return code, I could just do
curl -o /dev/null -s -w "%{http_code}\n" http://localhost
Related Topics
How to Make a Built-In Device Driver in Linux
Colored Shell Script Output Library
Skipping Acquire of Configured File 'Main/Binary-I386/Packages'
In Linux Determine If a .A Library/Archive 32-Bit or 64-Bit
How the Util of iOStat Is Computed
How to Make and Apply Svn Patch
Rtmp: Is There Such a Linux Command Line Tool
Docker Load and Save: "Archive/Tar: Invalid Tar Header"
Running as a Host User Within a Docker Container
Choosing Between Multiple Executables with Same Name in Linux
How to Group by Column Values into Row and Column Header and Then Sum the Value
Bash Script to Calculate Time Elapsed
Difference Between Unix Domain Stream and Datagram Sockets
How to Find a File/Directory That Could Be Anywhere on Linux Command Line
How to Load Luks Passphrase from Usb, Falling Back to Keyboard