Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I get the HTTP responses from bash script with curl command?

I have the following command that I execute from the shell:

curl -v -s -X GET -H "UserName: myUsername" -H "Password: myPassword" 
https://example.com

and from terminal I have the HTTP/1.1 200 response with different response parameters:

HTTP/1.1 200 OK
Date: Fri, 23 Oct 2015 10:04:02 GMT
Name: myName
Age: myAge

...

Now, in my .sh file i want take name and age so have the values in my variables $name and $age. My initial idea was to redirect stdout to a file to parse:

curl -v -s -X GET -H "UserName: myUsername" -H "Password: myPassword"
https://example.com > out.txt

but the file is empty.

Some ideas to have a variables in bash script enhanced by the HTTP response?

EDIT: the variables that I want to take are in the header of the response

Thanks

like image 599
PIppo Avatar asked Oct 23 '15 10:10

PIppo


1 Answers

You have to add --write-out '%{http_code}' to the command line and curl will print the HTTP_CODE to stdout.

e.g.curl --silent --show-error --head http://some.url.com --user user:passwd --write-out '%{http_code}'

However I don't know all the other "variables" you can print out.

like image 186
Angel O'Sphere Avatar answered Nov 14 '22 23:11

Angel O'Sphere