doubenggua9430 2017-10-13 12:56
浏览 167
已采纳

curl - 从cookie-jar文件中选择特定的cookie值

How do you select the value of a specific cookie from a cookie-jar file for future requests?

I need to submit a form on a website that requires that the current sessionId be passed as form data. The sessionId is set as a cookie on the login page.

First request (authentication)

curl -d "username=my_user&pass=my_pass" http://mywebsite.com/login.php -c, --cookie-jar  my-cookies

Second request (form submit)

curl -d "sessionId=jk5lkdr7cdqkn1ptqa0rmndbr7&formField1=abc&formField2=def" http://mywebsite.com/getReport.php -b, --cookie my-cookies

In the above request data, I've manually copied the session id from the cookie file. The cookie file looks like this. (sessionId = PHPSESSID)

# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

mywebsite.com   FALSE   /   FALSE   0   PHPSESSID   jk5lkdr7cdqkn1ptqa0rmndbr7
.mywebsite.com  TRUE    /   FALSE   1516507485  info    crownandcaliber%40mediaiqdigital.com
.mywebsite.com  TRUE    /   FALSE   1507867485  pref    deleted
.mywebsite.com  TRUE    /   FALSE   0   affiliate_flag  Y3Jvd25hbmRjYWxpYmVyQG1lZGlhaXFkaWdpdGFsLmNvbQ%3D%3D
.mywebsite.com  TRUE    /   FALSE   0   cookie  name%3ATm8gYWR2ZXJ0aXNlciBuYW1lIGF2YWlsYWJsZQ%3D%3D%3B

Is there a way for me to progammatically pull the value of the PHPSESSID cookie which is set in the first request into the form data for the second request?

</div>
  • 写回答

1条回答 默认 最新

  • dsafgdafgdf45345 2017-10-13 15:11
    关注

    The code below reads a 5th line from a file youfilewithcookie, gets content of a 7th column and reads it in a variable called sessId.

    awk 'NR==5' youfilewithcookie | awk {'print $7'} | (read sessId; yourculcommand here with $sessId)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制