代码之家  ›  专栏  ›  技术社区  ›  jamiet

如何检查HTTP状态代码并分析负载

  •  0
  • jamiet  · 技术社区  · 6 年前

    curl -s https://cat-fact.herokuapp.com/facts/random?animal=cat | jq .
    

    注意,我希望通过将响应传递给 jq .

    exit 1 视情况而定(例如,对于404或503)。我在网上搜索发现 https://superuser.com/a/442395/722402 这表明 --write-out "%{http_code}" 不过,在打印有效负载后打印http_代码可能很有用:

    curl -s --write-out "%{http_code}" https://cat-fact.herokuapp.com/facts/random?animal=cat | jq .
    

    $curl-s—写出“%{http_code}” https://cat-fact.herokuapp.com/facts/random?animal=cat |jq公司。

    “\u id”:“591f98783b90f7150a19c1ab”,
    “\u v”:0,
    “文本”:“猫和小猫应尽可能成对获得,因为猫的家庭最好成对互动。”,

    “createdAt”:“2018-01-04T01:10:54.673Z”,
    “删除”:错误,
    “type”:“猫”,

    “使用”:错误
    }

    我真正想要的是仍然输出有效负载,但是仍然能够检查http状态代码并相应地失败。我是个酒鬼,所以很难搞清楚。请帮忙?


    最新消息,我已经把索塔的工作拼凑在一起了。我想。虽然不太优雅,但我在找更好的。

    func() {
       echo "${@:1:$#-1}";
    }
    response=$(curl -s --write-out "%{http_code}" https://cat-fact.herokuapp.com/facts/random?animal=cat | jq .) 
    http_code=$(echo $response | awk '{print $NF}')
    func $response | jq .
    if [ $http_code == "503" ]; then
        echo "Exiting with error due to 503"
        exit 1
    elif [ $http_code == "404" ]; then
        echo "Exiting with error due to 404"
        exit 1
    fi
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   OkieOth    6 年前

    这个怎么样。它使用一个临时文件。看起来我有点复杂,但它分离了你的内容。

    # copy/paste doesn't work with the following
    curl -s --write-out \
       "%{http_code}" https://cat-fact.herokuapp.com/facts/random?animal=cat | \ 
       tee test.txt | \      # split output to file and stdout
       sed -e 's-.*\}--' | \ # remove everything before last '}'
       grep 200  && \       # try to find string 200, only in success next step is done
       echo && \            # a new-line to juice-up the output
       cat test.txt | \     # 
       sed 's-}.*$-}-' | \  # removes the last line with status
       jq                   # formmat json
    

    这里是复制/粘贴版本

    curl -s --write-out "%{http_code}" https://cat-fact.herokuapp.com/facts/random?animal=cat | tee test.txt | sed -e 's-.*\}--' | grep 200  && echo && cat test.txt | sed 's-}.*$-}-' | jq
    
        2
  •  1
  •   Matias Barrios    6 年前

    这是我的尝试。希望对你也有用。

    #!/bin/bash
    
    result=$( curl -i -s 'https://cat-fact.herokuapp.com/facts/random?animal=cat'  )
    
    
    status=$( echo "$result" | grep -E '^HTTPS?/[1-9][.][1-9] [1-9][0-9][0-9]' | grep -o ' [1-9][0-9][0-9] ')
    payload=$( echo "$result" | sed -n '/^\s*$/,//{/^\s*$/ !p}' )
    
    echo "STATUS : $status"
    echo "PAYLOAD : $payload"
    

    输出

    STATUS :  200
    PAYLOAD : {"_id":"591f98803b90f7150a19c23f","__v":0,"text":"Cats can't taste sweets.","updatedAt":"2018-12-05T05:56:30.384Z","createdAt":"2018-01-04T01:10:54.673Z","deleted":false,"type":"cat","source":"api","used":false}
    

    AWK版本

    payload=$( echo "$result" | awk '{ if( $0 ~ /^\s*$/ ){ c_p = 1 ; next; } if (c_p) { print $0} }' )
    

    当做!

    -我

    编辑二:从有效载荷中删除空行

    编辑三:包括 在情况下提取有效载荷的方法 是有问题的