NewBie Question : Parsing HTML response and retrieving a value

Hello,

Working on my first APIGEE proxy, searched in this community for answers, not able to make any break thru, any direction this community can provide will be very helpful.

Back End returns a very lengthy html page along with token. APIGEE needs to retrieve token ('X-CSRF-Token') to proceed on for further operations. Since response is in html, i am not entirely how to proceed on it.

Please let me know the direction i should on this.

<html>  <head> <title></title> </style> </head>         
<body>               
 <script>                       
               var xhr = new XMLHttpRequest();
                xhr.onload = function() {
                    location.reload(true);
                }
                xhr.open('POST', '/sap/hana/xs/formLogin/logout.xscfunc');
                xhr.setRequestHeader('X-CSRF-Token', 'D056AC37A7F86F4A45FE5DDA841');
                xhr.send();                 
</script></body></html>
0 3 253
3 REPLIES 3

No, don't do that.

Parsing HTML is a bad idea in general.

Parsing HTML in an Apigee proxy is not any better. Any time you're parsing an HTML page, it's probably a fragile, unreliable way to build an API.


What are you REALLY trying to do?

Not applicable

you are doing a post-call in which you are sending the token in header., what you can do is make the post call to api proxy in apigee and for further processing you can configure the backend in apigee or if it has multiple api calls dependncy before going to target, you can use service callout as well.

As Dino mentioned parsing or manipulating HTML in proxy is not a good idea. However you extract X-CSRF-Token using Extract Variable. You can extract value something like below -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-ExtractHtml">
    <DisplayName>EV-ExtractHtml</DisplayName>
    <Variable name="response.content">
        <Pattern>*'X-CSRF-Token', '{token}');*</Pattern>
    </Variable>
</ExtractVariables>

I have attached proxy that will extract X-CSRF-Token from request.

test-extract-html-api-rev1-2020-07-29.zip