Linux使用curl发送http get与post请求

curl命令是一个功能强大的网络工具,它能够通过http、ftp等方式下载文件,也能够上传文件。curl命令使用了libcurl库来实现,libcurl库常用在C程序中用来处理HTTP请求,curlpp是libcurl的一个C++封装,这几个东西可以用在抓取网页、网络监控等方面的开发,而curl命令可以帮助来解决开发过程中遇到的问题。开发人员可以使用curl来测试API接口,查看响应头和发出HTTP请求,curl可以让你不需要浏览器也能作为Http客户端发送请求。而且它是跨平台的,Linux、Windows、Mac都会执行的很好。

使用curl发送http get与post请求

curl发送get请求

当我们不加任何选项使用 curl 时,默认会发送 GET 请求来获取链接内容到标准输出。

curl http://baidu.com

多个参数

curl  http://11.120.12.89:6666/sengMsg?phone=18790987654&name=lily&msg=aaa

注意:有多个参数时需要把&转义一下,否则获取不到之后参数会报错

curl发送post请求

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

#例子1
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'username=cavan' -d 'password=123456' http://www.example.com

#例子2
curl http://11.120.12.89:6666/sengMsg -X POST -d "parameterName1=parameterValue1&parameterName2=parameterValue2"

post请求类型为application/json

#例子1
curl http://11.120.12.89:6666/sengMsg -X POST -H "Content-Type:application/json" -d '{"parameterName1":"parameterValue1","parameterName2":"parameterValue2"}'

#例子2
curl -X POST -H 'content-type: application/json' -d '{ "lockerCode":"BYK001","keyNumber":"2"}' http://192.168.0.194:4500/openKeyLock

post请求类型为multipart/form-data

#使用该-F选项时,curl使用 Content-Type 为“multipart/form-data”发送数据。
curl -X POST -F 'name=Jason' -F 'email=jason@example.com' https://example.com/contact.php

参数说明:

  • -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://google.com

    移除User-Agent标头

    $ curl -A '' https://google.com
  • -b:向服务器发送 Cookie生成一个标头Cookie: foo=bar,向服务器发送一个名为foo、值为bar的 Cookie。读取本地文件cookies.txt,里面是服务器设置的 Cookie

    $ curl -b 'foo=bar' https://google.com
    $ curl -b 'foo1=bar' -b 'foo2=baz' https://google.com
    $ curl -b cookies.txt https://www.google.com
  • -c:将服务器设置的 Cookie 写入一个文件。将服务器的 HTTP 回应所设置 Cookie 写入文本文件cookies.txt。

    $ curl -c cookies.txt https://www.google.com
  • -d:发送 POST 请求的数据体,请求携带的参数,多个参数使用&分隔

  • –data-urlencode:–data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。发送的数据hello world之间有一个空格,需要进行 URL 编码。

    $ curl --data-urlencode 'comment=hello world' https://google.com/login
  • -e:设置 HTTP 的标头Referer,表示请求的来源。将Referer标头设为https://google.com?q=example

    curl -e 'https://google.com?q=example' https://www.example.com
  • -F:向服务器上传二进制文件。传输文件,指定 MIME 类型,指定文件名。给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。

    $ curl -F 'file=@photo.png' https://google.com/profile

    -F参数可以指定 MIME 类型。指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream。

    $ curl -F 'file=@photo.png;type=image/png' https://google.com/profile

    -F参数也可以指定文件名。原始文件名为photo.png,但是服务器接收到的文件名为me.png。

    $ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
  • -G:构造 URL 的查询字符串。上面命令会发出一个 GET 请求,实际请求的 URL 为https://google.com/search?q=kitties&count=20。如果省略–G,会发出一个 POST 请求。

    $ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search

    如果数据需要 URL 编码,可以结合–data–urlencode参数。

    $ curl -G --data-urlencode 'comment=hello world' https://www.example.com
  • -H:指定请求头,例如 -H “Content-type:application/json”多个请求头传递多-H即可

  • -i:打印出服务器回应的 HTTP 标头。命令收到服务器回应后,先输出服务器回应的标头,然后空一行,再输出网页的源码。

    $ curl -i https://www.example.com
  • -k:k参数指定跳过 SSL 检测。

    $ curl -k https://www.example.com

    上面命令不会检查服务器的 SSL 证书是否正确。

  • -L:L参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。

  • -O:把输出写到该文件中,保留远程文件的文件名。后面的url要具体到某个文件,不然抓不下来。

    $ curl -O http://www.baidu.com/img/bdlogo.gif
  • -s:从文件中读取变量

    $ curl -s "@data.txt" http://www.example.com/login
  • -u:通过服务端配置的用户名和密码授权访问

    [root@linuxcool ~]# curl -u root https://www.linuxprobe.com/
    Enter host password for user 'root':
  • -v:显示详细的请求信息

  • -X:指定请求的方法,POST外还可以指定PUT等请求方法

  • -y:放弃限速所要的时间,默认为30

  • -Y:停止传输速度的限制,速度时间

要查看curl的全部参数用法,请输入 curl -h进行查看。

$ curl -h

原文始发于微信公众号(面试技术):Linux使用curl发送http get与post请求

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/187046.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!