Home > Software design >  How to print first line of the response and status code in curl
How to print first line of the response and status code in curl

Time:08-02

curl -o /dev/null -s -w "HTTPCode=%{http_code}_TotalTime%{time_total}s\n" http://test.com

just gives me the status code

I also want the body of the response, How can i do that in the command?

CodePudding user response:

Don't send it to /dev/null? -o is throwing it away.

curl -sw "HTTPCode=%{http_code}_TotalTime%{time_total}s\n" http://test.com

If you ONLY want the first line, as your title suggests, filter it.

curl -sw "HTTPCode=%{http_code}_TotalTime%{time_total}s\n" http://test.com | sed -n '1p; $p;'

This tells sed to print the first and last lines, because you asked for the first one, and -w prints after completetion. My test:

$: curl -s -w "HTTPCode=%{http_code}_TotalTime%{time_total}s\n" google.com | sed -n '1p; $p;'
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
HTTPCode=301_TotalTime0.270267s

If you specifically mean the first line of the body from the response, now you need to define "first line" a little, and you should really get an HTML-aware parser. I could probably do it in sed, but it's really kind of a bad idea in most cases.

If that's really what you need, let me know, supply some additional details, and we'll work on it.

CodePudding user response:

You can use this curl awk solution:

curl -sw "HTTPCode=%{http_code}_TotalTime%{time_total}s\n" 'http://test.com' |
awk '{p=p $0 ORS} END{print; sub(/\n[^\n] \n$/, "", p); print p}'
  • Related