Linux常用命令-curl

CURL 是一个强大的命令行工具,用于在终端中与网络资源进行交互,支持多种协议和定制选项,非常适合开发者和系统管理员进行网络调试和数据传输操作。

基本用法

  1. 发送GET请求

    curl http://example.com
    

    http://example.com 发送一个简单的 GET 请求,并输出响应内容到标准输出。

  2. 保存响应到文件

    curl -o output.txt http://example.com
    

    http://example.com 的响应保存到 output.txt 文件中。

  3. 发送POST请求并设置请求头

    curl -X POST -H "Content-Type: application/json" -d '{"key": "value"}' http://example.com/api
    

    这会向 http://example.com 发送一个 POST 请求,包含 {"key": "value"} 的数据体。

  4. 下载文件

    curl -O http://example.com/file.zip
    

    下载 http://example.com/file.zip 文件,并保存在当前目录下。

高级用法

  • HTTP方法:使用 -X 参数指定 HTTP 请求方法(GET、POST、PUT 等)。
  • 身份验证:支持 -u 参数用于基本的 HTTP 身份验证。
  • 使用代理:通过 -x 参数设置代理服务器。
  • SSL选项:支持 -k 参数来跳过 SSL 证书验证。
  • Cookie管理:使用 -b-c 参数来发送和保存 Cookie。

使用示例

  • 使用 Basic Auth 发送请求:

    curl -u username:password http://example.com/api
    
  • 使用代理发送请求:

    curl -x proxy_server:port http://example.com
    
  • 忽略 SSL 证书验证

    curl -k -X POST -H "Content-Type: application/json" -d '{"key": "value"}' https://example.com/api/test
    
  • Cookie管理

    # 发送请求时携带 Cookie
    curl -b "session_id=abcdef1234567890" http://example.com/protected/resource
    # 保存服务器返回的 Cookie 到文件
    curl -c cookies.txt http://example.com/login
    # 同时发送和保存 Cookie
    curl -b "session_id=abcdef1234567890" -c cookies.txt http://example.com/protected/resource
    
posted @ 2024-07-09 13:23  rustling  阅读(45)  评论(0编辑  收藏  举报