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

0. 场景

服务器分为多个区,每个区有不同的功能,部署好项目后需要从web区测试下联通性和应用能不能正常返回信息。

1. curl发送get请求

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

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

2. curl发送post请求

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

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

-d参数可以读取本地文本文件的数据,向服务器发送

$ curl -d '@data.txt' https://google.com/login

post请求类型application/json,

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

使用POST接口上传文件,@后传文件

curl  http://11.120.12.89:6666/sengMsg -F raw=@/tmp/raw.data -v
参数说明
-d请求携带的参数,多个参数使用&分隔
-X指定请求的方法,POST外还可以指定PUT等请求方法
-H指定请求头,例如 -H "Content-type:application/json"多个请求头传递多-H即可
-v显示详细的请求信息
-F传输文件,指定 MIME 类型,指定文件名
-O把输出写到该文件中,保留远程文件的文件名
-u通过服务端配置的用户名和密码授权访问
-A指定客户端的用户代理标头,即User-Agent,curl 的默认用户代理字符串是curl/[version]。
-b向服务器发送 Cookie
-c将服务器设置的 Cookie 写入一个文件
-d发送 POST 请求的数据体
–data-urlencode–data-urlencode参数等同于-d,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。
-e设置 HTTP 的标头Referer,表示请求的来源
-F向服务器上传二进制文件
-G构造 URL 的查询字符串
-i打印出服务器回应的 HTTP 标头

将下载的数据写入到文件,必须使用文件的绝对地址:

curl https://www.linuxcool.com/abc.txt --silent -O

访问需要授权的页面时,可通过-u选项提供用户名和密码进行授权:

[root@linuxcool ~]# curl -u root https://www.linuxprobe.com/
 Enter host password for user 'root':

-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

–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

-H参数可以通过直接添加标头Referer,达到同样效果。

curl -H 'Referer: https://google.com?q=example' https://www.example.com

-F参数用来向服务器上传二进制文件。给 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

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

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

-I参数向服务器发出 HEAD 请求,然会将服务器返回的 HTTP 标头打印出来。令输出服务器对 HEAD 请求的回应。–head参数等同于-I。

 $ curl -I https://www.example.com
  $ curl --head https://www.example.com

-k参数指定跳过 SSL 检测。

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

上面命令不会检查服务器的 SSL 证书是否正确。
-L参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。

$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet--limit-rate--limit-rate

用来限制 HTTP 请求和回应的带宽,模拟慢网速的环境。

$ curl --limit-rate 200k https://google.com

上面命令将带宽限制在每秒 200K 字节。
-o参数将服务器的回应保存成文件,等同于wget命令。

 $ curl -o example.html https://www.example.com

上面命令将www.example.com保存成example.html。
-O参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。

 $ curl -O https://www.example.com/foo/bar.html

上面命令将服务器回应保存成文件,文件名为bar.html。
-s参数将不输出错误和进度信息。

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

上面命令一旦发生错误,不会显示错误信息。不发生错误的话,会正常显示运行结果。如果想让 curl 不产生任何输出,可以使用下面的命令。

$ curl -s -o /dev/null https://google.com

-S参数指定只输出错误信息,通常与-s一起使用。

 $ curl -s -o /dev/null https://google.com

上面命令没有任何输出,除非发生错误。
-u参数用来设置服务器认证的用户名和密码。

  $ curl -u 'bob:12345' https://google.com/login

上面命令设置用户名为bob,密码为12345,然后将其转为 HTTP 标头Authorization: Basic Ym9iOjEyMzQ1。curl 能够识别 URL 里面的用户名和密码。

$ curl https://bob:12345@google.com/login

上面命令能够识别 URL 里面的用户名和密码,将其转为上个例子里面的 HTTP 标头。

$ curl -u 'bob' https://google.com/login

上面命令只设置了用户名,执行后,curl 会提示用户输入密码。
-v参数输出通信的整个过程,用于调试。
$ curl -v https://www.example.com–trace
参数也可以用于调试,还会输出原始的二进制数据。

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

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

   $ curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com

上面命令指定 HTTP 请求通过myproxy.com:8080的 socks5 代理发出。如果没有指定代理协议,默认为 HTTP。 $ curl -x james:cats@myproxy.com:8080 https://www.example.com上面命令中,请求的代理使用 HTTP 协议。

  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中,使用curl命令进行POST请求可以通过以下方式实现。首先,使用-H选项设置请求头的Content-Type为application/json,然后使用-X选项指定请求方法为POST,最后使用-d选项传递JSON数据作为请求体。例如,可以使用以下命令进行POST请求curl -H "Content-Type: application/json" -X POST -d '{"key1":"value1", "key2":"value2"}' https://example.com/api 其中,-H选项用于设置请求头,-X选项用于指定请求方法,-d选项用于传递JSON数据。请将https://example.com/api替换为实际的API地址,并将'{"key1":"value1", "key2":"value2"}'替换为实际的JSON数据。\[3\] #### 引用[.reference_title] - *1* [Linux | 如何使用 cURL 发送 POST 请求](https://blog.csdn.net/alexwei2009/article/details/125230019)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [linux使用curl发送http get与post请求](https://blog.csdn.net/qq_35456400/article/details/108790518)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [linux 服务器执行post请求 curl命令详解](https://blog.csdn.net/weixin_43882515/article/details/129408209)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值