简体   繁体   中英

How to print first line of the response and status code in curl

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?

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.

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}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM