GetHeader and multiple same key
I need to retrieve a cookie after a call to a Rest api.
Not a problem i thought, i'll use aqHttpResponse.GetHeader("Set-Cookie");
But how to retrieve the wanted one if multple Set-Cookie are in the response ?
All headers sent back:
Connection: keep-alive
Date: Mon, 16 Mar 2020 15:36:13 GMT
Content-Length: 3235
Content-Type: text/html;charset=UTF-8
Server: nginx
Set-Cookie: rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Sun, 15-Mar-2020 15:36:13 GMT
Set-Cookie: JSESSIONID=E525712AB14D70C27C8C13F4F063A5B3; Path=/; Secure; HttpOnly
X-Robots-Tag: noindex, nofollow
Strict-Transport-Security: max-age=31536000
I want the JSESSIONID
If i use the GetHeader i have ony the rememberMe.
I know that a solution is aqHttpResponse.AllHeaders and i parse it to find my JSESSIONID but is there any simplier and more elegant solution to achieve that ?
So it's bullet-proof working now ...
On same headers, call of extractKeyValueFromHeaders(aqHttpResponse.AllHeaders, "JSESSIONID")
returns
"5119F6E864B904735B8870DA08215BCD"
Call of extractKeyValueFromHeaders(aqHttpResponse.AllHeaders, "JSESSIONID", true)returns
"JSESSIONID=5119F6E864B904735B8870DA08215BCD; Path=/; Secure; HttpOnly"
The revised code is (algorithm working, optimization possible) :
function extractKeyValueFromHeaders(Headers = "", Key = "", AllValues = false) { if (Headers == "") throw Error("extractKeyValueFromHeaders() - Header is empty"); if (Key == "") throw Error("extractKeyValueFromHeaders() - Key is missing"); let headerValue = ""; Headers = Headers.replace(/\r/g, ''); let data = Headers.split("\n"); let pos1, pos2, len; for (let i = 0; i < data.length; i++) { if (data[i] != "") { pos1 = aqString.Find(data[i], ":"); if (pos1 != -1) { pos1 = aqString.Find(data[i], Key, pos1); if (pos1 != -1) { if (AllValues) { pos2 = data[i].length; len = pos1; } else { pos2 = aqString.Find(data[i], ";", pos1); if (pos2 == -1) pos2 = data[i].length; len = pos1 + Key.length + 1; } headerValue = data[i].substr(len, pos2 - len); break; } } } } return headerValue; }