使用curl发送请求
get请求
curl http://10.109.39.119/test/test2?name=name1
备注get请求是没有请求体的,如果一些特殊的协议中需要使用带有请求体的get请求加上参数-G
post请求
-d 指定post请求请求体,也就是数据
不指定Content-Type的时候,默认是application/x-www-form-urlencoded
#普通post 请求
curl http://10.109.39.119/test/t5 -X POST -d 'data=d1&name=name1'
#等价普通post请求
curl http://10.109.39.119/test/t5 -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'data=d1&name=name1'
备注:post请求地址后面不能带参数,否者会被认定为get请求
json请求
-H 指定请求头,json请求需要指定Content-Type: application/json
curl -X POST http://10.109.39.119/format/json -H "Content-Type: application/json" -d '{"a":"string","b":0,"date":"2025-02-28T08:21:32.994Z"}'
保存响应到文件
-o指定相应保存的文件路径
curl http://10.109.39.119/test/test2?name=name1 -o response.txt
cookie相关
--cookie等价与-b,后面可以跟cookie文件或者是字符串的cookie
-c后面跟的是一个文件,这个文件里面保持的是cookie的内容
#指定cookie
curl http://10.109.39.119/test/test2?name=name1 --cookie "c1=v1"
# -b 和 --cookie 都能指定
curl http://10.109.39.119/test/test2?name=name1 -b "c1=v1"
#保存cookie
curl http://10.109.39.119/test/test2?name=name1 -c cookie.txt
#请求的时候带上cookie,并且保存返回的cookie
curl http://10.109.39.119/test/test2?name=name1 -c cookie.txt --cookie cookie.txt
下面是一份cookie文件,前面是注释,后面第6行是一条cookie
[root@localhost ~]# more cookie.txt
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
10.109.39.119 FALSE /test/ FALSE 0 user zhangsan
能耍的时候就一定要耍,不能耍的时候一定要学。
--天道酬勤,贵在坚持posted on 2025-02-28 18:13 zhangyukun 阅读(250) 评论(0) 收藏 举报
浙公网安备 33010602011771号