Author |
Message
|
saviobarr |
Posted: Thu Mar 10, 2016 9:41 am Post subject: Manipulating HTTP headers |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
Hi everyone,
I have a requirement to extract a particular field (Set-Cookie field) value from a HTTP response header , and then set another HTTP request header (Cookie), using this value. First I tried to navigate thru the response using InputRoot.HTTPResponseHeader.Set-Cookie, but it does not work.
I built a code that loops InputRoot.HTTPResponseHeader.*[] and verifies each value in order to identify the desired field, but it does not look elegant at all. Does anybody have a suggestion how to obtain the value of Set-Cookie in a message like below?
Message
HTTPResponseHeader
X-AspNet-Version:CHARACTER:2.0.50727
Set-Cookie:CHARACTER:ASP.NET_SessionId=lvtmx
Cache-Control:CHARACTER:private, max-age=0
Many thanks in advance
Savio Barros |
|
Back to top |
|
 |
mqjeff |
Posted: Thu Mar 10, 2016 9:59 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
What does "it doesn't work" mean?
What does your code look like?
use [ c o d e ] tags. _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
saviobarr |
Posted: Thu Mar 10, 2016 10:02 am Post subject: |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
I have to improve my ESQL skills... the only thing I had to do is place the field name between double quotes
InputRoot.HTTPResponseHeader."Set-Cookie"
Many thanks!
Savio |
|
Back to top |
|
 |
saviobarr |
Posted: Thu Mar 10, 2016 10:05 am Post subject: |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
mqjeff wrote: |
What does "it doesn't work" mean?
|
Compile error I was trying InputRoot.HTTPResponseHeader.Set-Cookie. I fixed it to InputRoot.HTTPResponseHeader."Set-Cookie"
Quote: |
What does your code look like?
use [ c o d e ] tags.
|
If I post the code here I will be banned from this forum . The ugliest code ever...
Savio Barros |
|
Back to top |
|
 |
mqjeff |
Posted: Thu Mar 10, 2016 11:11 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
You usually only get banned for consistently *not* posting code and diagnostics and etc. etc. etc.
Or at least, completely ignored, if not banned. _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
saviobarr |
Posted: Thu Mar 10, 2016 11:23 am Post subject: |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
mqjeff wrote: |
You usually only get banned for consistently *not* posting code and diagnostics and etc. etc. etc.
Or at least, completely ignored, if not banned. |
I better post the old code... . It works, but I fixed the code just surrounding the field with double quotes, in a single line.
Code: |
CREATE PROCEDURE findCookie()BEGIN
DECLARE I INTEGER 1;
DECLARE elements INTEGER;
DECLARE cookie CHARACTER '';
SET elements = CARDINALITY(InputRoot.HTTPResponseHeader.*[]);
seachCookie : REPEAT
IF CONTAINS( CAST(InputRoot.HTTPResponseHeader.*[I] AS CHARACTER), 'ASP.NET_SessionId') THEN
SET cookie = InputRoot.HTTPResponseHeader.*[I];
SET cookie = SUBSTRING(cookie BEFORE ';');
SET OutputRoot.HTTPRequestHeader.Cookie = cookie;
LEAVE seachCookie;
END IF;
SET I = I + 1;
UNTIL
I = elements
END REPEAT seachCookie;
END;
|
|
|
Back to top |
|
 |
mqjeff |
Posted: Thu Mar 10, 2016 11:57 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
-5 points for using cardinality.
 _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
timber |
Posted: Fri Mar 11, 2016 1:10 am Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
Why is this CAST required?
Code: |
CAST(InputRoot.HTTPResponseHeader.*[I] AS CHARACTER |
|
|
Back to top |
|
 |
saviobarr |
Posted: Fri Mar 11, 2016 1:29 am Post subject: |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
timber wrote: |
Why is this CAST required? |
HTTP headers have non-character values in it (ex.: Content-Length and Date fields)
Code: |
CAST(InputRoot.HTTPResponseHeader.*[I] AS CHARACTER |
|
|
Back to top |
|
 |
timber |
Posted: Fri Mar 11, 2016 2:25 am Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
Fair enough - so they appear as ESQL types INTEGER and TIMESTAMP in the message tree. You need to CAST to CHARACTER to ensure that the CONTAINS function will not complain. |
|
Back to top |
|
 |
saviobarr |
Posted: Fri Mar 11, 2016 4:06 am Post subject: |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
mqjeff wrote: |
-5 points for using cardinality.
 |
Hi mqjeff, thanks for replying. I am far from being a ESQL expert... is it a bad practice use of cardinality? I've been through the Knowledge Center, and found no scenarios where cardinality is not recommended.
Great weekend for everyone
Savio Barros |
|
Back to top |
|
 |
timber |
Posted: Fri Mar 11, 2016 5:47 am Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
Use a FOR loop to iterate over an array of same-named sibling nodes. CARDINALITY is slower, and the syntax is messier. It's all described in the ESQL documentation: search for 'ESQL FOR statement' |
|
Back to top |
|
 |
Vitor |
Posted: Fri Mar 11, 2016 5:53 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
saviobarr wrote: |
I am far from being a ESQL expert... is it a bad practice use of cardinality? |
It's not a great idea. Mostly, it encourages the use of an index, as you're doing it. That's problematic because of how IIB calculates index position; from scratch every time. So to find the first element, it counts 1, the second element it starts back at the top of the tree, counts 1 element and uses the next. For the 100th element it starts back at the top of the tree, counts 1, counts 2, counts 3, etc and you can get a cup of coffee before it finds the 100th. Any of the ESQL references stay where they're put, so it's much more efficient to point a reference at the 1st element, move it to the next, move it to the next and so on.
This is especially important when you're not just looking for data (as you are) but deleting elements ("trimming the tree"). If you delete element 3 and you're using an index, element 4 becomes element 3 because of the counting I describe above and that can give odd results. It certainly means your loop will abend because you'll try to access an index value that's larger than the number of elements in the tree. Again, not a problem if you're using a reference variable. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
|