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" -c, --cookie-jar  my-cookies

Second request (form submit)

curl -d "sessionId=jk5lkdr7cdqkn1ptqa0rmndbr7&formField1=abc&formField2=def" -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
# This file was generated by libcurl! Edit at your own risk.   FALSE   /   FALSE   0   PHPSESSID   jk5lkdr7cdqkn1ptqa0rmndbr7  TRUE    /   FALSE   1516507485  info  TRUE    /   FALSE   1507867485  pref    deleted  TRUE    /   FALSE   0   affiliate_flag  Y3Jvd25hbmRjYWxpYmVyQG1lZGlhaXFkaWdpdGFsLmNvbQ%3D%3D  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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


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