在日常 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 等。 |
常用命令示例如下:
- 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 也可缺省。
- 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 不带引号时,需要加转义字符 \,否则不能正确识别参数。
- 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
- 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 不可缺省。
- 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 不可缺省。
- 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 可以缺省。
文章参考: