我使用curl来获取http报头以查找http状态代码并返回响应。我使用命令获取http头信息

curl -I http://localhost

为了得到响应,我使用命令

curl http://localhost

一旦使用了-I标志,我就只得到了头信息,响应就不再存在了。是否有一种方法可以同时获得http响应和头/http状态码在一个命令?


当前回答

哇,这么多答案,cURL开发人员肯定把它留给了我们作为家庭练习:)好吧,这是我的想法-一个脚本,使cURL工作,因为它应该是,即:

像cURL那样显示输出。 如果HTTP响应代码不在2XX范围内,则使用非零代码退出

保存为curl-wrapper.sh:


#!/bin/bash

output=$(curl -w "\n%{http_code}" "$@")
res=$?

if [[ "$res" != "0" ]]; then
  echo -e "$output"
  exit $res
fi

if [[ $output =~ [^0-9]([0-9]+)$ ]]; then
    httpCode=${BASH_REMATCH[1]}
    body=${output:0:-${#httpCode}}

    echo -e "$body"

    if (($httpCode < 200 || $httpCode >= 300)); then
        # Remove this is you want to have pure output even in 
        # case of failure:
        echo
        echo "Failure HTTP response code: ${httpCode}"
        exit 1
    fi
else
    echo -e "$output"
    echo
    echo "Cannot get the HTTP return code"
    exit 1
fi

所以它就像往常一样,但不是curl do ./curl-wrapper.sh:

所以当结果在200-299范围内时:

./curl-wrapper.sh www.google.com 
# ...the same output as pure curl would return...
echo $?
# 0

当结果超出200-299范围时:

./curl-wrapper.sh www.google.com/no-such-page
# ...the same output as pure curl would return - plus the line
#    below with the failed HTTP code, this line can be removed if needed:
#
# Failure HTTP response code: 404
echo $?
# 1

只是不要传递“-w|——write-out”参数,因为这是脚本中添加的内容

其他回答

对于编程使用,我使用以下代码:

curlwithcode() {
    code=0
    # Run curl in a separate command, capturing output of -w "%{http_code}" into statuscode
    # and sending the content to a file with -o >(cat >/tmp/curl_body)
    statuscode=$(curl -w "%{http_code}" \
        -o >(cat >/tmp/curl_body) \
        "$@"
    ) || code="$?"

    body="$(cat /tmp/curl_body)"
    echo "statuscode : $statuscode"
    echo "exitcode : $code"
    echo "body : $body"
}

curlwithcode https://api.github.com/users/tj

显示如下信息:

statuscode : 200
exitcode : 0
body : {
  "login": "tj",
  "id": 25254,
  ...
}

我使用这个命令打印状态代码,而不输出任何其他结果。此外,它将只执行HEAD请求并遵循重定向(分别为-I和-L)。

curl -o -I -L -s -w "%{http_code}" http://localhost

这使得检查运行状况脚本中的状态代码非常容易:

sh -c '[ $(curl -o -I -L -s -w "%{http_code}" http://localhost) -eq 200 ]'

在末尾追加一行“http_code:200”,然后grep关键字“http_code:”并提取响应代码。

result=$(curl -w "\nhttp_code:%{http_code}" http://localhost)

echo "result: ${result}"   #the curl result with "http_code:" at the end

http_code=$(echo "${result}" | grep 'http_code:' | sed 's/http_code://g') 

echo "HTTP_CODE: ${http_code}"  #the http response code

在这种情况下,您仍然可以使用非静默模式/ verbose模式来获取有关请求的更多信息,例如curl响应体。

仅捕获响应: curl——location——请求GET“http://localhost:8000” 捕获响应和它的状态码: curl——location——request GET "http://localhost:8000" -w "%{http_code}" 在文件中捕获响应: curl——location——request GET "http://localhost:8000" -s -o "response.txt"

我用过这个:

    request_cmd="$(curl -i -o - --silent -X GET --header 'Accept: application/json' --header 'Authorization: _your_auth_code==' 'https://example.com')"

获取HTTP状态

    http_status=$(echo "$request_cmd" | grep HTTP |  awk '{print $2}')
    echo $http_status

为了获得响应体,我使用了这个

    output_response=$(echo "$request_cmd" | grep body)
    echo $output_response