I had the same issue with the stderr output retrieved from the curl command. I found that the output contains carriage return that needs to be removed. For the example above this could be done using the tr tool:
for data in $records ;
do
data=$(echo "$data" | tr -d '\r')
test+="$data"
done