简体   繁体   English

如何在 curl 中打印响应和状态码的第一行

[英]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?不要将它发送到/dev/null? -o is throwing it away. -o正在扔掉它。

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.这告诉 sed 打印第一行和最后一行,因为您要求第一行,并且-w在完成后打印。 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.如果您特别指的是响应中正文的第一行,那么现在您需要稍微定义“第一行”,并且您应该真正获得一个支持 HTML 的解析器。 I could probably do it in sed , but it's really kind of a bad idea in most cases.可能可以在sed中做到这一点,但在大多数情况下这确实是个坏主意。

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 + awk解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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