首页 文章

如何使用cURL调试CORS请求?

提问于
浏览
212

如何使用cURL调试CORS请求?到目前为止,我找不到任何“模拟”预检请求的方法 .

3 回答

  • 376

    以下是使用curl调试CORS请求的方法 .

    Sending a regular CORS request using cUrl:

    curl -H "Origin: http://example.com" --verbose \
      https://www.googleapis.com/discovery/v1/apis?fields=
    

    -H "Origin: http://example.com" 标志是发出请求的第三方域 . 替换你的域名 .

    --verbose 标志打印出整个响应,以便您可以看到请求和响应标头 .

    我上面使用的网址是对支持CORS的Google API的示例请求,但您可以替换您正在测试的任何网址 .

    响应应包含 Access-Control-Allow-Origin 标头 .

    Sending a preflight request using cUrl:

    curl -H "Origin: http://example.com" \
      -H "Access-Control-Request-Method: POST" \
      -H "Access-Control-Request-Headers: X-Requested-With" \
      -X OPTIONS --verbose \
      https://www.googleapis.com/discovery/v1/apis?fields=
    

    这看起来类似于常规的CORS请求,但有一些补充:

    -H 标志向服务器发送额外的预检请求标头

    -X OPTIONS 标志表示这是HTTP OPTIONS请求 .

    如果预检请求成功,则响应应包括 Access-Control-Allow-OriginAccess-Control-Allow-MethodsAccess-Control-Allow-Headers 响应标头 . 如果预检请求未成功,则这些 Headers 不应为200 .

    您还可以使用 -H 标志指定其他标头,例如 User-Agent .

  • 0

    更新了涵盖大多数情况的答案

    curl -H "Access-Control-Request-Method: GET" -H "Origin: http://localhost" --head http://www.example.com/
    
    • http://localhost替换为您要测试的URL .

    • 如果响应包含 Access-Control-Allow-* ,那么您的资源支持CORS .

    Rationale for alternative answer

    我不时地谷歌这个问题,接受的答案永远不是我需要的 . 首先它打印响应体,这是很多文本 . 添加 ---head 仅输出 Headers . 其次,在测试S3网址时,我们需要提供额外的标头 -H "Access-Control-Request-Method: GET" .

    希望这会节省时间 .

  • 18

    下面的bash脚本"corstest"适合我 . 它基于上面的Jun's评论 .

    usage

    corstest [-v] url

    examples

    ./corstest https://api.coindesk.com/v1/bpi/currentprice.json
    https://api.coindesk.com/v1/bpi/currentprice.json Access-Control-Allow-Origin: *
    

    阳性结果显示为绿色

    ./corstest https://github.com/IonicaBizau/jsonrequest
    https://github.com/IonicaBizau/jsonrequest does not support CORS
    you might want to visit https://enable-cors.org/ to find out how to enable CORS
    

    否定结果显示为红色和蓝色

    -v选项将显示完整的curl标头

    corstest

    #!/bin/bash
    # WF 2018-09-20
    # https://stackoverflow.com/a/47609921/1497139
    
    #ansi colors
    #http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
    blue='\033[0;34m'  
    red='\033[0;31m'  
    green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
    endColor='\033[0m'
    
    #
    # a colored message 
    #   params:
    #     1: l_color - the color of the message
    #     2: l_msg - the message to display
    #
    color_msg() {
      local l_color="$1"
      local l_msg="$2"
      echo -e "${l_color}$l_msg${endColor}"
    }
    
    
    #
    # show the usage
    #
    usage() {
      echo "usage: [-v] $0 url"
      echo "  -v |--verbose: show curl result" 
      exit 1 
    }
    
    if [ $# -lt 1 ]
    then
      usage
    fi
    
    # commandline option
    while [  "$1" != ""  ]
    do
      url=$1
      shift
    
      # optionally show usage
      case $url in      
        -v|--verbose)
           verbose=true;
           ;;          
      esac
    done  
    
    
    if [ "$verbose" = "true" ]
    then
      curl -s -X GET $url -H 'Cache-Control: no-cache' --head 
    fi
    origin=$(curl -s -X GET $url -H 'Cache-Control: no-cache' --head | grep Access-Control)
    
    
    if [ $? -eq 0 ]
    then
      color_msg $green "$url $origin"
    else
      color_msg $red "$url does not support CORS"
      color_msg $blue "you might want to visit https://enable-cors.org/ to find out how to enable CORS"
    fi
    

相关问题