Extract both custom HTTP header values and the body from a request
The following script uses cURL to fetch a web page, and then extracts both an HTTP from the response headers, as well as the body.
This script uses awk, which creates temporary files to store header and body part.
#!/bin/bash
function extractHeaders {
local curlResponse="$1"
local tempF="$( mktemp )"
local tH="${tempF}headers"
local tB="/dev/null"
echo "${response}" | awk -v bl=1 "$( printf 'bl{bl=0; h=($0 ~ /HTTP\/1/)} /^\r?$/{bl=1} {print $0>(h?"%s":"%s")}' $tH $tB )"
headerContents="$( cat "${tH}" ; rm "${tH}" )"
echo "${headerContents}"
}
function extractBody {
local curlResponse="$1"
local tempF="$( mktemp )"
local tH="/dev/null"
local tB="${tempF}body"
echo "${response}" | awk -v bl=1 "$( printf 'bl{bl=0; h=($0 ~ /HTTP\/1/)} /^\r?$/{bl=1} {print $0>(h?"%s":"%s")}' $tH $tB )"
bodyContents="$( cat "${tB}" ; rm "${tB}" )"
echo "${bodyContents}"
}
someURL="https://www.google.com/"
response="$( curl \
--silent --include \
--request GET --url "${someURL}" )"
body="$( extractBody "${response}" )"
headers="$( extractHeaders "${response}" )"
someHeader="Content-Type"
someHeaderValue="$( echo "${headers}" | grep "^${someHeader}:" | sed -E 's/^(\S+?): (.+)/\2/' )"
echo "The ${someHeader} was ${someHeaderValue}"
echo "The body was ${body}"
Links
When I want to completely see the traffic originating from my cURL instance, I use (a Windows-based HTTP(s)-proxy GUI). Fiddler can be configured to decrypt TLS (https://) traffic, but that means that the server certificate for cURL will be untrusted. The following args instruct cURL to use a local (untrusted) HTTPs-proxy: