使用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 閱讀(251) 評論(0) 收藏 舉報
浙公網安備 33010602011771號