cURL如何只获取http请求的code信息

参考:http://beerpla.net/2010/06/10/how-to-display-just-the-http-response-code-in-cli-curl/


仅获取结果响应码

curl -s -w "%{http_code}" 'url' -o /dev/null
  • 命令解释

-s表示slient,不显示进度或错误信息。如:

$ curl  -w "%{http_code}\\n" 'http://www.baidu.com/' -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed100 53917    0 53917    0     0  4467k      0 --:--:-- --:--:-- --:--:-- 51.4M
200

$ curl -s -w "%{http_code}\\n" 'http://www.baidu.com/' -o /dev/null
200

-w定义在请求成功结束后显示的信息,即请求成功后格式化输出的内容。语法:

-w/--write-out 
format可以为多个变量组成的字符串,亦可为读取的文件(@filename),也可以指定从标准输入读取(@-)。变量书写格式为%{variable_name},若要输出%可以用%%。如:
    curl -w "%{http_code}-%{time_total}" "url"

$ curl -w "%{http_code}-%{time_total}" "h.baidu.com/" -o /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--  100 57733    0 57733    0     0  5188k      0 --:--:-- --:--:-- --:--:-- 27.5M
200-0.011

变量列表直接查看cURL命令中-w命令解释。

-o将结果输出到文件而非标准输出。



你可能感兴趣的:(linux,cURL)