PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

[FIXED] How to get a cookie value in google apps script?

 July 23, 2022     cookies, google-apps-script, json     No comments   

Issue

I'm having difficulty parsing a JSON string with nested arrays. here is an example of the JSON

const allHeaders = {"Strict-Transport-Security":"max-age=31536000","Timing-Allow-Origin":"*","s_ip":"1.5.295.221","Server":"Asia","Vary":"Accept-Encoding","x-application-context":"shop-buyer-s.7001","s_group":"buyer-sessio","Content-Language":"zh-CN","Access-Control-Allow-Headers":"Origin, x-ua, x-umidtoken, x-csrf-token, X-Requested-With, Content-Type, Accept","Set-Cookie":["hms_cid=80dcf576-a581-4d43-8302-1605fe36565c; Domain=.hms.zn; Expires=Sun, 09-Jul-2023 04:39:13 GMT; Path=/","hms_sid=112120422020a413958ddf33139b516b; Domain=.hms.zn; Path=/; HttpOnly","_tb_token_=e8eafb9bebe63; Domain=.hms.zn; Path=/; HttpOnly"],"Transfer-Encoding":"chunked","object-status":"ttl=2592000,age=42,gip=121.43.99.68","Connection":["keep-alive","Transfer-Encoding"],"s_tag":"285873024335988|0^|^^","s_tid":"21410a7016573415536295456ef332","eagleeye-traceid":"21410a7016573415536295456ef332","s_v":"4.0.2.0","Content-Encoding":"gzip","P3P":"CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'","Date":"Sat, 09 Jul 2022 04:39:13 GMT","Access-Control-Max-Age":"3600","s_ucode":"Asia:CENTER","Access-Control-Allow-Methods":"POST, GET, OPTIONS, DELETE","Content-Type":"text/html;charset=UTF-8","s_status":"STATUS_NOT_EXISTED"}

I need to get the number hms_sid= exmple: 112120422020a413958ddf33139b516b

I'm trying const sid = allHeaders['Set-Cookie']


Solution

I'm pretty sure google apps script is just javascript so here's my code:

let cookie = allHeaders['Set-Cookie'][1];
let tokens = cookie.split(";");
let sidString = tokens[0].split("=");
let sid = sidString[1];

Here is how I did it:

Given:

const allHeaders = {
    "Strict-Transport-Security": "max-age=31536000",
    "Timing-Allow-Origin": "*",
    "s_ip": "1.5.295.221",
    "Server": "Asia",
    "Vary": "Accept-Encoding",
    "x-application-context": "shop-buyer-s.7001",
    "s_group": "buyer-sessio",
    "Content-Language": "zh-CN",
    "Access-Control-Allow-Headers": "Origin, x-ua, x-umidtoken, x-csrf-token, X-Requested-With, Content-Type, Accept",
    "Set-Cookie": ["hms_cid=80dcf576-a581-4d43-8302-1605fe36565c; Domain=.hms.zn; Expires=Sun, 09-Jul-2023 04:39:13 GMT; Path=/", "hms_sid=112120422020a413958ddf33139b516b; Domain=.hms.zn; Path=/; HttpOnly", "_tb_token_=e8eafb9bebe63; Domain=.hms.zn; Path=/; HttpOnly"],
    "Transfer-Encoding": "chunked",
    "object-status": "ttl=2592000,age=42,gip=121.43.99.68",
    "Connection": ["keep-alive", "Transfer-Encoding"],
    "s_tag": "285873024335988|0^|^^",
    "s_tid": "21410a7016573415536295456ef332",
    "eagleeye-traceid": "21410a7016573415536295456ef332",
    "s_v": "4.0.2.0",
    "Content-Encoding": "gzip",
    "P3P": "CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'",
    "Date": "Sat, 09 Jul 2022 04:39:13 GMT",
    "Access-Control-Max-Age": "3600",
    "s_ucode": "Asia:CENTER",
    "Access-Control-Allow-Methods": "POST, GET, OPTIONS, DELETE",
    "Content-Type": "text/html;charset=UTF-8",
    "s_status": "STATUS_NOT_EXISTED"
}

I pulled the cookie the same that you did:

let cookie = allHeaders['Set-Cookie'];

The issue with this is that it returns:

[
  'hms_cid=80dcf576-a581-4d43-8302-1605fe36565c; Domain=.hms.zn; Expires=Sun, 09-Jul-2023 04:39:13 GMT; Path=/',
  'hms_sid=112120422020a413958ddf33139b516b; Domain=.hms.zn; Path=/; HttpOnly',
  '_tb_token_=e8eafb9bebe63; Domain=.hms.zn; Path=/; HttpOnly'
]

Using typeof I was able to determine this is an Object type.

let cookie = allHeaders['Set-Cookie'];
console.log(typeof(cookie));
object

Since this is an Object I enumerated the keys:

console.log(Object.keys(cookie));
[ '0', '1', '2' ]

Now I'm able to match the comma separated values with an object key. This means I can get only the cookie value with the info I need:

let cookie = allHeaders['Set-Cookie'][1];

Which outputs:

hms_sid=112120422020a413958ddf33139b516b; Domain=.hms.zn; Path=/; HttpOnly

After that I checked the type of the object again with typeof to find out it's a String so I used .split(";") string function to tokenize the string:

[
  'hms_sid=112120422020a413958ddf33139b516b',
  ' Domain=.hms.zn',
  ' Path=/',
  ' HttpOnly'
]

we only care about the first token and we want to tokenize that again by =:

let sidString = tokens[0].split("=");
[ 'hms_sid', '112120422020a413958ddf33139b516b' ]

And there we have it, we can get the value with sidString[1]



Answered By - Elizabeth
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing