2014-03-19 139 views
21

卷曲调用不带参数,我得到的页面输出,即使HTTP状态码= 404:获取页面的输出,卷曲--fail

$ curl http://www.google.com/linux; 
<!DOCTYPE html> 
<html lang=en> 
    <meta charset=utf-8> 
    <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"> 
    <title>Error 404 (Not Found)!!1</title> 
    <style> 
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/errors/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px} 
    </style> 
    <a href=//www.google.com/><span id=logo aria-label=Google></span></a> 
    <p><b>404.</b> <ins>That’s an error.</ins> 
    <p>The requested URL <code>/linux</code> was not found on this server. <ins>That’s all we know.</ins> 

$ echo $?; 
0 

状态代码为0

调用它与--fail不会显示输出:

$ curl --fail http://www.google.com/linux; 
curl: (22) The requested URL returned error: 404 Not Found 

$ echo $?; 
22 

的状态代码是22,现在...

ID”像即使在http状态= 404,500(如第一次执行curl)时也能得到输出,并同时获得不同的系统错误(如在第二次curl执行中,$? = 22)。 卷曲可能吗?如果没有,我怎么能实现这个与另一个工具(这个工具必须接受文件上传和发布数据!wget似乎并不是一种替代方案...)

谢谢。

回答

12

首先,错误代码(或退出代码)的最大值是255。这里是reference

此外,--fail不会允许你做你正在寻找的东西。但是,您可以使用其他方式(编写shell脚本)来处理这种情况,但不确定它会对您有用或不起作用!

http_code=$(curl -s -o out.html -w '%{http_code}' http://www.google.com/linux;) 

if [[ $http_code -eq 200 ]]; then 
    exit 0 
fi 

## decide which status you want to return for 404 or 500 
exit 204 

现在做$?,你会从那里得到退出代码。

您会在out.html文件中找到响应html。

您也可以将url作为命令行参数传递给脚本。 Check here

3

不幸的是不可能卷曲。但你可以用wget来做到这一点。

$ wget --content-on-error -qO- http://httpbin.org/status/418 

    -=[ teapot ]=- 

     _...._ 
    .' _ _ `. 
    | ."`^`". _, 
    \_;`"---"`|// 
     |  ;/ 
     \_  _/ 
     `"""` 
$ echo $? 
8