curl

  官网:https://curl.se/ 

  1、什么是curl

    curl 在命令行或脚本中用于传输数据。

    curl 是一种用于从服务器传输数据或向服务器传输数据的工具

    1997年首次发行。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称cURL为下载工具。cURL还包含了用于程序开发的libcurl

    curl支持的通信协议有DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP.

    curl还支持SSL认证、HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, HTTP/3, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, SCRAM-SHA, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more

 

  2、下载安装

    Linux或Mac系统本身即支持curl

    Windows系统:

      下载地址:https://curl.se/download.html

      ①:下载curl 

      ②:安装cygwin,然后选择curl作为cygwin安装的一部分(推荐)

 

  3、使用

    输入man curl 查看curl的所有用法,man page:https://curl.se/docs/manpage.html

    格式:curl [options / URLs]

curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'

      ①:-X 参数指定HTTP请求的方法,通常是GET、POST、PUT;如-X POST 或 -XPOST;默认是GET,可省略-X参数

      ②:-A 参数指定客户端的用户代理请求头,即User-Agent。curl 的默认用户代理字符串是curl/[version]

        如将User-Agent改成Chrome浏览器:$ curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://www.baidu..com

        移除User-Agent请求头:curl -A '' https://google.com

        也可以通过 -H 参数直接指定请求,更改User-Agent:curl -H 'User-Agent: php/1.0' https://www.baidu..com

       ③:-b 参数用来向服务器发送Cookie

        curl -b 'foo=bar' https://www.baidu..com

      ④:-c 参数将服务器返回设置的Cookie写入到一个文件中

        如将HTTP响应所设置的Cookie写入文件cookies.txt中:curl -c cookies.txt https://www.baidu..com

      ⑤:-d 参数用于发送POST请求的数据体

        使用-d参数后,HTTP请求会自动加上请求头:Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST

        如:curl -d 'login=emma&password=123' https://www.baidu..com

      ⑥:--data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码

      ⑦:-G 参数用来构造URL的查询字符串

        curl -G -d 'id=123' -d 'name=yang' https://www.baidu..com

        上面的命令会发出一个GET请求,实际请求的URL为https://www.baidu.com?id=123&name=yang

        如果省略-G 将会发送一个POST请求

      ⑧:-x 参数指定HTTP请求的代理

 

 

    示例,发送POST请求,Content-Type: application/json:

curl -X POST "localhost:9200/_security/user/jacknich/_password?pretty" -H 'Content-Type: application/json' -d'
{
  "password" : "new-test-password"
}
'

 

   

END.

posted @ 2022-01-06 15:35  杨岂  阅读(364)  评论(0编辑  收藏  举报