doubenggua9430
doubenggua9430
2017-10-13 12:56

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 dsafgdafgdf45345 4年前

    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)
    
    点赞 评论 复制链接分享

相关推荐