【问题标题】:Get page output with curl --fail使用 curl --fail 获取页面输出
【发布时间】:2014-04-25 16:58:50
【问题描述】:

不带参数调用 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 status = 404、500 时获得输出(如第一次 curl 执行),同时获得不同的系统错误(如在第二次 curl 执行中,$? = 22) . 卷曲可以吗?如果没有,我怎么能用另一个工具来实现这一点(这个工具必须接受文件上传和发布数据!wget 似乎不是替代品......)

谢谢。

【问题讨论】:

    标签: http curl http-post wget postfile


    【解决方案1】:

    首先,错误代码(或退出代码)的最大值是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.

    【讨论】:

    • 真的很棒!谢了!
    【解决方案2】:

    不幸的是,curl 不可能。但是你可以用 wget 做到这一点。

    $ wget --content-on-error -qO- http://httpbin.org/status/418
    
        -=[ teapot ]=-
    
           _...._
         .'  _ _ `.
        | ."` ^ `". _,
        \_;`"---"`|//
          |       ;/
          \_     _/
            `"""`
    $ echo $?
    8
    

    【讨论】:

    • 注意需要wget 1.14+
    【解决方案3】:

    我找到了解决办法,因为 wget 不适合发送 multipart/form-data

    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
    

    说明

    -o - -w "\n%{http_code}\n" - 输出到标准输出(实际上是通过管道传输到下一个命令),最后带有状态码
    tee - 输出将通过管道传输到下一个命令并另外打印到标准输出
    @987654325 @ - 从最后一行提取状态码
    cmp &lt;(echo 2xx) - 比较状态码,仅第一个字符
    grep "char 2" - 如果第一个字符需要为 2,否则失败

    在shell脚本中你也可以做更好的比较(目前它只允许2xx,所以像300这样的重定向被处理为错误cmp上面是如何使用的)

    【讨论】:

    • 当有 200 响应时它不是假设不会失败吗?导致调用 httpbin.org/status/200 时之前的评论确实失败
    • 你到底是什么意思?我打印的 curl 命令没有失败 200
    • 它在我的机器上确实失败了。 ?
    • @SeB.Fr 是对的。它在 2xx 状态范围内以非零代码退出。至少在 macos 上。
    • 需要将 grep "char 2" 更改为 grep "byte 2"。现在可以使用了。
    【解决方案4】:

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

    curl_fail_with_body() {
      curl -o - -w "\n%{http_code}\n" "$@" | awk '{l[NR] = $0} END {for (i=1; i<=NR-1; i++) print l[i]}; END{ if ($0<200||$0>299) exit $0 }'
    }
    
    # example usage
    curl_fail_with_body -sS http://httpbin.org/status/418
    

    说明

    • -o - -w "\n%{http_code}\n" - 打印到标准输出(实际上是通过管道传送到下一个命令),最后带有状态码
    • {l[NR] = $0} END {for (i=1; i&lt;=NR-1; i++) print l[i]} - 打印除最后一行之外的所有行
    • END{ if ($0&lt;200||$0&gt;299) exit $0 } - 如果last line != 2xx 将以非零代码退出

    替代版本,如果要在命令后输出错误码:
    END{ if ($0&lt;200||$0&gt;299) {print "The requested URL returned error: " $0; exit 1}


    顺便说一句,curl 从 v7.76.0 开始支持 --fail-with-body 选项。
    此选项允许您在不使用外部工具的情况下实现所需的行为。

    【讨论】:

    • 很好的解决方案,谢谢。 Nit:退出代码范围是 0-255,因此任何 HTTP 状态代码 > 255 都不会作为退出代码出现。我把它改成了exit 1 vs. exit $0
    【解决方案5】:

    现在可以使用 curl。从 7.76.0 版开始,您可以这样做

    curl --fail-with-body ...
    

    这完全符合 OP 的要求:显示文档正文并以代码 22 退出。

    https://curl.se/docs/manpage.html#--fail-with-body

    【讨论】:

      【解决方案6】:

      这是我的解决方案 - 它使用 jq 并假设正文是 json

      #  this code adds a statusCode field to the json it receives and then jq squeezes them together
      # curl 7.76.0 will have curl --fail-with-body and thus eliminate all this
        local result
        result=$(
          curl -sL -w ' { "statusCode": %{http_code}} ' -X POST "${headers[@]}" "${endpoint}" \
            -d "${body}"  "$curl_opts" | jq -ren '[inputs] | add'
        )
      #   always output the result
        echo "${result}"
      #  jq -e will produce an error code if the expression result is false or null - thus resulting in a
      # error return code from this function naturally. This is much preferred rather than assume/hardcode
      # the existence of a error object in the body payload
        echo "${result}" | jq -re '.statusCode >= 200 and .statusCode < 300' > /dev/null
      

      【讨论】:

        猜你喜欢
        • 2013-07-08
        • 2014-01-17
        • 1970-01-01
        • 2011-10-23
        • 2011-04-11
        • 2010-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多