curl命令行传递Cookie参数的方法【20221009】

发表于 2021-07-27 15:55:32
阅读 173

介绍

介绍

福哥要在使用curl请求接口的时候加上cookie参数怎么办?

教程

格式

其实通过curl --help就能知道怎么弄了。

home/topic/2022/1008/16/55ae804bec539d09ff81196c8ff93d46.png

通过字符串传递

可以直接传递cookie字符串,格式就是param=value&param2=value2...这样的。

curl -i --cookie "uid=35;uname=tongfu.net" "http://localhost/api/status"

home/topic/2022/1008/16/0b0ac99398616c3b03e60754775abd01.png

通过文件传递

编辑一个cookie.txt文本文件,加入cookie设置内容,格式如下:

[域名]    FALSE    [URI]    FALSE    [过期时间Unix时间戳]    [变量名称]    [变量值]

福哥把前面的例子写成了cookie.txt格式,内容如下:

localhost	FALSE	/	FALSE	0	uid	35
localhost	FALSE	/	FALSE	0	uname	tongfu.net

现在福哥使用cookie.txt文件想接口传递cookie参数。

curl -i --cookie cookie.txt "http://localhost/api/status"

home/topic/2022/1008/16/676f9dc1ec862985217ba881f65038af.png

获取服务器端Cookie

如果想要得到服务器端的cookie值的话,我们可以通过--cookie-jar将它存下来。

curl -i --cookie cookie.txt --cookie-jar cookie-resp.txt "http://localhost/api/status"

home/topic/2022/1008/16/281d4eaf198cd6e7f06a4179fae0380e.png