How to extract text from bulk text

I need to get a value from a variable but the content can’t be parsed automatically (json conversion fails and searches fail too).

Indeed, this content is usually displayed using standard browser and executed on windows load (this sends back the variable to be used later).
As Postman doesn’t generate this event, I don’t receive this variable and can’t extract it from the text.

In below an example, I need to extract data.access_token value :

    <script type="text/javascript">

    /**
     * Check whether this page is loaded in an iframe or a popup.
     */
    function isInIframe() {
        try {
            return window.self !== window.top;
        } catch (e) {
            return true;
        }
    }

    /**
     * Create the message payload to send back to the parent window.
     */
    function buildMessageData() {
        var data = {};

        
            data.access_token = '1024_random_characters';
            data.token_type = 'Bearer';
            data.expires_in = '900';
            data.scope = 'openid\x20email\x20roles\x20tenant';
        
            data.state = 'state';
        

        return data;
    }

    /**
     * On window load, post the message payload back to the parent window.
     */
    function onWindowLoadHandler() {
        var parentWindow = window.isInIframe() ? window.parent : window.opener;
        
        parentWindow.postMessage(buildMessageData(), 'https\x3A\x2F\x2Fmyurl.com');
    }

    // Attach event handler for the 'DOM loaded' or equivalent event on the window
    if (window.addEventListener) {
        window.addEventListener('DOMContentLoaded', onWindowLoadHandler, false);
    } else if (window.attachEvent) {
        // IE8
        window.attachEvent('onload', onWindowLoadHandler);
    }
</script>
</head>
<body>

Thanks in advance.

How does your response look like? Could you provide a sample by using https://www.mocky.io/ and specifying what you want to extract?

Hi,

My response looks like the following : http://www.mocky.io/v2/5d074e5d3000009c54051f0f
I need to extract this “random_text_1024_Characters” in data.access_token

Thanks in advance for your help.

Hi @thomas_g,

not sure it´s the best solution, but you could use a regular expression:

var regEx = new RegExp("data.access_token = '(.*?)'");
var resultArray = regEx.exec(responseBody);
var access_token = resultArray[1];

Edit: I think what you´re looking for, is Javascript parsing in Postman. Doing a quick google search on that, I wasn´t succesful. Maybe you´re more lucky.
Usually parsing with RegEx is quite brittle, and should not be the preferred solution.

Best,
Christian

Thanks a lot Christian, even if it’s not the preferred solution as you said it works :smiley:
Do you know if I can use “sed” like command to modify a variable?

For instance my variable contains ‘\x2D’ instead of ‘-’

Thanks,

Thomas

To me, this sounds more like an encoding problem, I´m not a Javascript expert, but I guess Javascript has commands to handle that.

But I guess replace() (JavaScript String replace() Method) would also do the job.

Best,
Christian

Your mocky URL doesn’t work for me :confused:
But I’d use replace() like @chrissbaumann mentioned, but that’s just my preference.

Didn´t work for me in Chrome either, but when I called it from Postman it did. :wink:

1 Like