CURL命令

发布时间 2023-05-22 16:36:53作者: God-slayer

1. 概述

工作中排查线上问题时,会经常调用问题接口,使用postman太笨重了,且客户不一定能安装这类软件,因此此处记录下使用curl命令调用接口,curl命令调用接口不复杂,记录于此是为了自己找的方便

2. 请求post body

curl -X POST http://ip:port/hello/hello.action -H 'Content-Type: application/json' -d '{"name": "AABBCCDD","age": 18,"address": "南极"}'

-H: 加入header参数

-d: 请求对象的json字符串

2.1. 字符串太长问题

如果字符串太长,可以将json字符串改成一个data.json 文件放在服务器上,命令变成

curl -X POST http://ip:port/hello/hello.action -H 'Content-Type: application/json' -d @data.json

2.2. 参数太多的问题

如果curl 参数太多,比如 -X -H -d .... 太多,导致curl太长,可以考虑使用换行,或者使用文件记录

换行

curl http://ip:port/hello/hello.action \
-X POST \
-H 'Content-Type: application/json' \
-d '{"name": "AABBCCDD","age": 18,"address": "南极"}'

或者把参数写在curl.options文件中

 curl -K curl.options  http://ip:port/hello/hello.action