常用的 Linux curl 发送 http 命令总结

26 篇文章 3 订阅
2 篇文章 0 订阅

  在日常 Java 接口测试中,Postman 图形界面工具较为常用,但是 Postman 非常耗费内存。

  curl 是常用的命令行工具,它的功能非常强大,命令行参数多达几十种,它的名字就是客户端(client)的 URL 工具的意思。curl 命令如果熟练的话,完全可以取代 Postman 来进行接口测试。

  curl 命令全部参数,参见 《curl [options…] <url> options 选项整理》,本文简单总结下常用的一些参数的意义,以及常用的 curl 发送 http 请求命令。

参数作用
-A指定客户端的用户代理标头,即 User-Agent。
curl 的默认用户代理字符串是 curl/[version]。
-b向服务器发送 Cookie。
-d指定发送的数据。
- -data-urlencode- -data-urlencode 参数等同于 -d,发送 POST 请求的数据。
区别在于会自动将发送的数据进行 URL 编码。
-H添加 HTTP 请求头。
-X指定 HTTP 请求的方法。
一般可选字段有 GET、POST、PUT、DELETE 等。

  常用命令示例如下:

  1. curl 发送 http get 请求,提交单个 form 表单数据:
$ curl -H "Content-Type: application/x-www-form-urlencoded" -X GET "http://localhost:7778/test3?age=12"

$ curl -H "Content-Type: application/x-www-form-urlencoded" "http://localhost:7778/test3?age=12"

$ curl "http://localhost:7778/test3?age=12"

$ curl http://localhost:7778/test3?age=12

  Content-Type 缺省值为 application/x-www-form-urlencoded,当提交表单数据时,默认以 GET 方式提交,此时 -X GET 也可缺省。

  1. curl 发送 http get 请求,提交多个 form 表单数据:
$ curl -H "Content-Type: application/x-www-form-urlencoded" -X GET "http://localhost:7778/test2?age=12&name=Lucy"

$ curl -H "Content-Type: application/x-www-form-urlencoded" "http://localhost:7778/test2?age=12&name=Lucy"

$ curl "http://localhost:7778/test2?age=12&name=Lucy"

$ curl http://localhost:7778/test2?age=12\&name=Lucy

  当提交多个 form 表单,且 URL 不带引号时,需要加转义字符 \,否则不能正确识别参数。

  1. curl 发送 http post 请求,提交单个 form 表单数据:
$ curl -H "Content-Type: application/x-www-form-urlencoded" -X POST "http://localhost:7778/test4" -d 'age=12'

$ curl -H "Content-Type: application/x-www-form-urlencoded" -X POST "http://localhost:7778/test4?age=12"

$ curl -X POST "http://localhost:7778/test4?age=12"

$ curl -X POST http://localhost:7778/test4?age=12
  1. curl 发送 http post 请求,提交多个 form 表单数据:
$ curl -H "Content-Type: application/x-www-form-urlencoded" -X POST "http://localhost:7778/test1" -d 'age=12&name=Lucy'

$ curl -H "Content-Type: application/x-www-form-urlencoded" -X POST "http://localhost:7778/test1?age=12&name=Lucy"

$ curl -H "Content-Type: application/x-www-form-urlencoded" -X POST "http://localhost:7778/test1" -d 'age=12' -d 'name=Lucy'

$ curl -X POST "http://localhost:7778/test1?age=12&name=Lucy"

$ curl -X POST "http://localhost:7778/test1" -d 'age=12&name=Lucy'

$ curl -X POST http://localhost:7778/test1?age=12\&name=Lucy

  可以多次使用 -d 选项来指定多个传输参数,也可以使用一次 -d 选项来指定多个参数。当 Content-Type 为 application/x-www-form-urlencoded 时,-X POST 不可缺省。

  1. curl 发送 http get 请求,提交 json 数据:
$ curl -H "Content-Type: application/json" -X GET "http://localhost:7778/test6" -d '{"age":"12","name":"Lucy"}'

$ curl -H "Content-Type: application/json" -X GET "http://localhost:7778/test6" -d '{"age":"12"}'

  当 Content-Type 为 application/json 时,默认以 POST 方式提交,此时 -X GET 不可缺省。

  1. curl 发送 http post 请求,提交 json 数据:
$ curl -H "Content-Type: application/json" -X POST "http://localhost:7778/test5" -d '{"age":"12","name":"Lucy"}'

$ curl -H "Content-Type: application/json" -X POST "http://localhost:7778/test5" -d '{"age":"12"}'

$ curl -H "Content-Type: application/json" "http://localhost:7778/test5" -d '{"age":"12","name":"Lucy"}'

  当 Content-Type 为 application/json 时,默认以 POST 方式提交,此时 -X POST 可以缺省。

  文章参考:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值