V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
wsgzao
V2EX  ›  Linux

curl 命令学习使用小结

  •  
  •   wsgzao ·
    wsgzao · 2020-02-10 12:20:52 +08:00 · 3755 次点击
    这是一个创建于 1536 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前言

    curl 是利用 URL 语法在命令行方式下工作的开源文件传输工具。它被广泛应用在 Unix、多种 Linux 发行版中,并且有 DOS 和 Win32、Win64 下的移植版本。在日常的开发和问题处理中,经常会使用 curl 命令来测试 http 接口,Windows/macOS 环境上有很多接口测试的工具比如 Postman,但这些工具在 Linux 平台上使用起来相对不那么便捷。有时候当我们要测试一些外部接口时,当本地无权调用测试路径时,需要将测试建立在 Linux 平台,除了封装简单的请求代码进行实现外,可通过 curl 工具实现。当然我们平时接触 curl 命令最多的地方还是在 Linux 环境,本来 curl 作为常用命令应该不用多介绍,但是在使用 Jenkins API 做数据迁移的过程中发现自己对 curl 相关的命令还是不熟悉,借这个机会重新梳理一些技巧。

    curl 命令学习使用小结

    更新历史

    2020 年 02 月 05 日 - 初稿

    阅读原文 - https://wsgzao.github.io/post/curl/


    curl 简介

    curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user interaction.

    curl offers a busload of useful tricks like proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer resume, Metalink, and more. As you will see below, the number of features will make your head spin!

    curl 支持你做很多事情。你可以把 curl 想象成一个精简的命令行网页浏览器。它支持几乎你能想到的所有协议,可以交互访问几乎所有在线内容。唯一和浏览器不同的是,curl 不会渲染接收到的相应信息。

    curl 常用命令

    curl 是一个利用 URL 语法在命令行下工作的文件传输工具。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称 curl 为下载工具。其语法格式及常见参数含义如下,

    # 语法
    curl [option] [url]
    
    # 最简单的使用,获取服务器内容,默认将输出打印到标准输出中(STDOUT)中。
    curl http://www.centos.org
    
    # 添加-v 参数可以看到详细解析过程,通常用于 debug
    curl -v http://www.centos.org
    
    # curl 发送 Get 请求
    curl URL
    curl URL -O 文件绝对路径
     
    # curl 发送 post 请求
    
    # 请求主体用 json 格式
    curl -X POST -H 'content-type: application/json' -d @json 文件绝对路径 URL
    curl -X POST -H 'content-type: application/json' -d 'json 内容' URL
    # 请求主体用 xml 格式
    curl -X POST -H 'content-type: application/xml' -d @xml 文件绝对路径 URL
    curl -X POST -H 'content-type: application/xml' -d 'xml 内容' URL
     
    # 设置 cookies
    curl URL --cookie "cookie 内容"
    curl URL --cookie-jar cookie 文件绝对路径
     
    # 设置代理字符串
    curl URL --user-agent "代理内容"
    curl URL -A "代理内容"
     
    # curl 限制带宽
    curl URL --limit-rate 速度
     
    # curl 认证
    curl -u user:pwd URL
    curl -u user URL
     
    # 只打印 http 头部信息
    curl -I URL
    curl -head URL
    
    # 末尾参数
    --progress  显示进度条
    --silent    不现实进度条
    
    # 不需要修改 /etc/hosts,curl 直接解析 ip 请求域名
    # 将 http://example.com 或 https://example.com 请求指定域名解析的 IP 为 127.0.0.1
    curl --resolve example.com:80:127.0.0.1 http://example.com/
    curl --resolve example.com:443:127.0.0.1 https://example.com/
    

    curl 接口测试

    curl 可以很方便地完成对 REST API 的调用场景,比如:设置 Header,指定 HTTP 请求方法,指定 HTTP 消息体,指定权限认证信息等。通过 -v 选项也能输出 REST 请求的所有返回信息。curl 功能很强大,有很多参数,这里列出 REST 测试常用的参数:

    -X/--request [GET|POST|PUT|DELETE|…]  指定请求的 HTTP 方法
    -H/--header                           指定请求的 HTTP Header
    -d/--data                             指定请求的 HTTP 消息体( Body )
    -v/--verbose                          输出详细的返回信息
    -u/--user                             指定账号、密码
    -b/--cookie                           读取 cookie  
    
    # 典型的测试命令为:
    curl -v -X POST -H "Content-Type: application/json" http://127.0.0.1:8080/user -d'{"username":"admin","password":"admin1234"}'...
    
    # 测试 get 请求
    curl http://www.linuxidc.com/login.cgi?user=test001&password=123456
    
    # 测试 post 请求
    curl -d "user=nickwolfe&password=12345" http://www.linuxidc.com/login.cgi
    
    # 请求主体用 json 格式
    curl -X POST -H 'content-type: application/json' -d @json 文件绝对路径 URL
    curl -X POST -H 'content-type: application/json' -d 'json 内容' URL
     
    # 请求主体用 xml 格式
    curl -X POST -H 'content-type: application/xml' -d @xml 文件绝对路径 URL
    curl -X POST -H 'content-type: application/xml' -d 'xml 内容' URL
    
    # 发送 post 请求时需要使用-X 选项,除了使用 POST 外,还可以使用 http 规范定义的其它请求谓词,如 PUT,DELETE 等
    curl -XPOST url
    
    #发送 post 请求时,通常需要指定请求体数据。可以使用-d 或--data 来指定发送的请求体。
    curl -XPOST -d "name=leo&age=12" url
    
    # 如果需要对请求数据进行 urlencode,可以使用下面的方式:
    curl -XPOST --data-urlencode "name=leo&age=12" url
    
    # 此外发送 post 请求还可以有如下几种子选项:
    –data-raw
    –data-ascii
    –data-binary
    
    

    使用 curl 和 Jenkins REST API

    # To retrieve the job config.xml
    curl -X GET '<jenkinshost>/job/<jobname>/config.xml' -u username:API_TOKEN -o <jobname>.xml
    
    # to use this config to create a new job
    curl -s -XPOST '<jenkinshost>/createItem?name=<jobname>' -u username:API_TOKEN --data-binary @<jobname>.xml -H "Content-Type:text/xml"
    
    # get all jenkins jobs
    curl -X GET '<jenkinshost>/api/json?pretty=true' -u username:API_TOKEN -o jobs.json
    
    # get jenkins view
    curl -X GET '<jenkinshost>/view/<viewname>/api/json' -u username:API_TOKEN -o view.json
    
    

    curl 练习

    21 个 curl 命令练习

    curl exercises

    curl 练习答案

    参考文章

    curl the man page

    linux 使用 curl 进行接口测试

    cURL 笔记

    5 条回复    2020-02-11 01:18:33 +08:00
    gearfox
        1
    gearfox  
       2020-02-10 13:53:05 +08:00
    谢谢分享
    wsgzao
        2
    wsgzao  
    OP
       2020-02-10 14:27:23 +08:00
    @gearfox #1 谢谢留言支持
    Owenjia
        3
    Owenjia  
       2020-02-10 14:48:30 +08:00
    --libcurl 选项挺实用的,可生成一个对应的 C 程序。
    Les1ie
        4
    Les1ie  
       2020-02-11 00:15:53 +08:00
    @Owenjia #3 试了下 有点好用 nb
    Honwhy
        5
    Honwhy  
       2020-02-11 01:18:33 +08:00
    谢谢分享
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3514 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:43 · PVG 08:43 · LAX 17:43 · JFK 20:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.