Http 使用curl--fail获取页面输出

Http 使用curl--fail获取页面输出,http,curl,http-post,wget,postfile,Http,Curl,Http Post,Wget,Postfile,在没有参数的情况下调用curl,即使使用http状态代码=404,也会得到页面输出: $ 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"> <t

在没有参数的情况下调用curl,即使使用http状态代码=404,也会得到页面输出:

$ 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状态=404500(类似于第一次curl执行)时获得输出,同时获得不同的系统错误(类似于第二次curl执行,$?=22)。 卷曲可以吗?如果没有,我如何用另一个工具实现这一点(这个工具必须接受文件上传和发布数据!wget似乎不是一个替代方案…)


谢谢。

首先,错误代码(或退出代码)的最大值为
255
。这是你的电话号码

另外,
--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作为命令行参数传递给脚本

不幸的是,不可能使用curl。但是你可以用wget来做这件事

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

    -=[ teapot ]=-

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

我找到了一个解决方案,因为wget不适合发送多部分/表单数据

curl -o - -w "\n%{http_code}\n" http://httpbin.org/status/418 | tee >(tail -n 1 | cmp <(echo 2xx) - ) | tee >(grep "char 2"; echo $? > status-code) && grep 0 status-code
curl-o--w“\n%{http\u-code}\n”http://httpbin.org/status/418 |tee>(tail-N1 | cmp(grep“char 2;echo$?>状态代码)和&grep 0状态代码
解释
-o--w“\n%{http\u code}\n”
-将状态代码打印到stdout(实际上它通过管道传输到下一个命令)
tee
-输出将通过管道传输到下一个命令,并额外打印到stdout
tail-n1
-从最后一行提取状态代码

cmp谢谢@timaschew,这是我基于纯awk的增强版:

curl\u fail\u with_body(){

curl-o--w“\n%{http_code}\n”“$@”awk'{l[NR]=$0}END{for(i=1;i说明您需要wget 1.14+,当有200个响应时,它不应该失败吗?因为前面的注释在调用时确实失败了。您的确切意思是什么?我打印的curl命令在200时没有失败,它在我的机器上确实失败。@SeB.Fr是正确的。它在2xx状态范围上以非零代码退出。至少在macos上。需要一个从
grep“char 2”
更改为
grep“byte 2”
。现在可以工作了。非常好!THX!非常好的解决方案,谢谢。Nit:退出代码的范围是0-255,因此任何HTTP状态代码>255都不会作为退出代码。我在使用中将其更改为
exit 1
vs.
exit$0