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

curl -I http://localhost

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

curl http://localhost

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


当前回答

我发现这个问题是因为我想要独立访问响应和内容,以便为用户添加一些错误处理。

Curl允许您自定义输出。您可以打印HTTP状态代码以std输出并将内容写入另一个文件。

curl -s -o response.txt -w "%{http_code}" http://example.com

这允许您检查返回代码,然后决定是否值得打印、处理、记录响应等。

http_response=$(curl -s -o response.txt -w "%{http_code}" http://example.com)
if [ $http_response != "200" ]; then
    # handle error
else
    echo "Server returned:"
    cat response.txt    
fi

%{http_code}是一个由curl代替的变量。你可以做更多的事情,或者发送代码到stderr,等等。参见curl手册和——write-out选项。

-w, --write-out Make curl display information on stdout after a completed transfer. The format is a string that may contain plain text mixed with any number of variables. The format can be specified as a literal "string", or you can have curl read the format from a file with "@filename" and to tell curl to read the format from stdin you write "@-". The variables present in the output format will be substituted by the value or text that curl thinks fit, as described below. All variables are specified as %{variable_name} and to output a normal % you just write them as %%. You can output a newline by using \n, a carriage return with \r and a tab space with \t. The output will be written to standard output, but this can be switched to standard error by using %{stderr}.

https://man7.org/linux/man-pages/man1/curl.1.html

其他回答

仅捕获响应: 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"

要获得响应代码和响应:

$ curl -kv https://www.example.org

要得到响应代码:

$ curl -kv https://www.example.org 2>&1 | grep -i 'HTTP/1.1 ' | awk '{print $3}'| sed -e 's/^[ \t]*//'

2>&1:错误存储在输出中,用于解析 Grep:从输出中过滤响应代码行 Awk:从响应代码行中过滤响应代码 Sed:删除前导空白

我发现这个问题是因为我想要独立访问响应和内容,以便为用户添加一些错误处理。

Curl允许您自定义输出。您可以打印HTTP状态代码以std输出并将内容写入另一个文件。

curl -s -o response.txt -w "%{http_code}" http://example.com

这允许您检查返回代码,然后决定是否值得打印、处理、记录响应等。

http_response=$(curl -s -o response.txt -w "%{http_code}" http://example.com)
if [ $http_response != "200" ]; then
    # handle error
else
    echo "Server returned:"
    cat response.txt    
fi

%{http_code}是一个由curl代替的变量。你可以做更多的事情,或者发送代码到stderr,等等。参见curl手册和——write-out选项。

-w, --write-out Make curl display information on stdout after a completed transfer. The format is a string that may contain plain text mixed with any number of variables. The format can be specified as a literal "string", or you can have curl read the format from a file with "@filename" and to tell curl to read the format from stdin you write "@-". The variables present in the output format will be substituted by the value or text that curl thinks fit, as described below. All variables are specified as %{variable_name} and to output a normal % you just write them as %%. You can output a newline by using \n, a carriage return with \r and a tab space with \t. The output will be written to standard output, but this can be switched to standard error by using %{stderr}.

https://man7.org/linux/man-pages/man1/curl.1.html

哇,这么多答案,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”参数,因为这是脚本中添加的内容

一行代码,只是为了获得状态代码:

curl -s -i https://www.google.com | head -1

将其更改为head -2也会给出时间。


如果你想要一个while-true循环,它会是:

URL="https://www.google.com"

while true; do
    echo "------"
    curl -s -i $URL | head -2
    sleep 2;
done

直到您执行cmd+C(或Windows中的ctrl+C)。

------
HTTP/2 200
date: Sun, 07 Feb 2021 20:03:38 GMT
------
HTTP/2 200
date: Sun, 07 Feb 2021 20:03:41 GMT
------
HTTP/2 200
date: Sun, 07 Feb 2021 20:03:43 GMT
------
HTTP/2 200
date: Sun, 07 Feb 2021 20:03:45 GMT
------
HTTP/2 200
date: Sun, 07 Feb 2021 20:03:47 GMT
------
HTTP/2 200
date: Sun, 07 Feb 2021 20:03:49 GMT