Capture curl header and response in environment variable Capture curl header and response in environment variable curl curl

Capture curl header and response in environment variable


I had this problem too and it was a serious head scratcher. I solved it by piping to tr and deleting \r characters like so:

foo=$(curl -i -H "Content-type: application/json" -X POST -d $JSON $URL | tr -d '\r')echo $foo


Put quotes around your variable.

$ foo=$(curl -i -H "Content-type: application/json" -X POST -d $JSON $URL)$ echo "$foo"HTTP/1.1 100 ContinueHTTP/1.1 200 OKDate: Thu, 09 Jan 2020 16:38:16 GMTContent-Length: 0