head=true while IFS= read -r line; do if $head; then if [[ -z $line ]]; then head=false else headers+=("$line") fi else body+=("$line") fi done < <(curl -sD - "$url" | sed 's/\r$//') printf "%s\n" "${headers[@]}" echo === printf "%s\n" "${body[@]}"
To combine array elements into one scalar variable:
the_body=$( IFS=$'\n'; echo "$body[*]" )
In bash 4.3, you can use named links to simplify the transition from header mode to body mode:
declare -n section=headers while IFS= read -r line; do if [[ $line = $'\r' ]]; then declare -n section=body fi section+=("$line") done < <(curl -sD - "$url")
For some reason, Glenn Jackman's answer did not catch part of the body of the answer. I had to split the curl request into another command extension and then enclose it in double quotes. Then I did not use arrays, but simply merged values ββwith variables. This works for me:
output=$(curl -si -d "" --request POST https://$url) head=true while read -r line; do if $head; then if [[ $line = $'\r' ]]; then head=false else header="$header"$'\n'"$line" fi else body="$body"$'\n'"$line" fi done < <(echo "$output")
Thanks Glenn!
source share