insomnia

insomnia how to extract the value from an input html response


I have the following HTML and I want to extract the value from the input with a name equal to code, using insomnia.

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Return</title>
        <script>
window.onload=function() {
    var form = document.getElementById("redirect_form");
    form.submit();
}
        </script>
    <SCRIPT type="text/javascript">
/*<![CDATA[*/ 
document.cookie = "IV_JCT=%2Foauth; path=/;secure";
/*]]>*/ 
</SCRIPT>
</head>
    <body>
      <p>Return....</p>
      <form id="redirect_form" name="redirect_form" action="https://example.com/login/callbackmfe" method="POST">
          <input type="hidden" name="code" value="yDCKqoCgXDAEdeY2d01ln43gaHerP2" />
      <input type="hidden" name="iss" value="https://example.com" />
      <input type="hidden" name="state" value="testing" />
      
      </form>
    </body>
</html>

I've already tried several ways: /html/body/input[@name='code']/@value OR (//input[@name='code'])[1]/@value

What would you be doing wrong?

Insomnia Version

Version: Insomnia 2023.2.2 V8: 10.8.168.20-electron.0

See the imagem


Solution

  • It is Insomnia defect. the <html> tag need <!DOCTYPE> Declaration. If have no !DOCTYPE can't parse.

    Your HTML tag missing !DOCTYPE

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    

    Insomnia recognized HTML tag

    <!DOCTYPE html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    

    demo server

    Save as server.js

    const express = require('express');
    const app = express();
    // //input[@type='hidden' and @name='code']/@value
    app.get('/data', (req, res) => {
        const htmlContent = `
        <!DOCTYPE html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                <title>Return</title>
                <script>
        window.onload=function() {
            var form = document.getElementById("redirect_form");
            form.submit();
        }
                </script>
                <script type="text/javascript">
                    document.cookie = "IV_JCT=%2Foauth; path=/;secure";
                </script>
            </head>
            <body>
                <p>Return....</p>
                <form id="redirect_form" name="redirect_form" action="https://example.com/login/callbackmfe" method="POST">
                    <input type="hidden" name="code" value="yDCKqoCgXDAEdeY2d01ln43gaHerP2" />
                    <input type="hidden" name="iss" value="https://example.com" />
                    <input type="hidden" name="state" value="testing" />
                </form>
            </body>
        </html>
        `;
        res.set('Content-Type', 'text/html');
        res.send(htmlContent);
    });
    
    app.listen(3000, () => {
        console.log('Server is running on port 3000');
    });
    

    Install dependencies

    npm install express
    

    Run Server

    node server.js
    

    Call by Insomnia

    enter image description here

    Parsing HTML by Insomnia

    enter image description here

    Detail steps in here

    XPath

    //input[@type='hidden' and @name='code']/@value
    

    enter image description here

    I am using this version. I hope to fix in the next version.

    enter image description here

    Note

    Postman can do by this Tests script

    var htmlResponse = pm.response.text();
    
    var codeValue = htmlResponse.match(/<input type="hidden" name="code" value="([^"]+)" \/>/);