Search code examples
regexsecurityjmetertokencsrf

How can I use RegEx to capture a security token for use within JMeter?


I am using JMeter's Regular Expression Extractor to capture a security token from the first HTTP response header to use in future requests.

The HTTP Response is as follows:

{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNVE8iLCJpYXQiOiIxNTEyMDcxNTg1Iiwic3ViIjoiOTEzNjQ0NCIsInV0ayI6InUvenBESVBzOCtUSlZUaW5tYVBaUjBqd2xyd2lJU1lSRVJ1ZnphNUVzS2s9IiwidmVyIjoiMi4wLjguNjIiLCJuYmYiOjE1MTIwNzEyODV9.4y__KpTFSgkdD_dMAuMiClbSfcmnvdtr0IEoVHFB_Fw","sessionTimeoutSeconds":1800,"profileRequirements":null}

So I'm trying to capture all the Characters where I wrote RANDOMCHARS

{"token":"RANDOMCHARS","sessionTimeoutSeconds":1800,"profileRequirements":null}

I plan to use this site to test any suggestions: https://regexr.com/

Any help would be so far beyond appreciated! =)


Solution

  • Code

    See regex in use here

    "token"\s*:\s*"([^"]*)"
    

    Results

    Input

    {"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNVE8iLCJpYXQiOiIxNTEyMDcxNTg1Iiwic3ViIjoiOTEzNjQ0NCIsInV0ayI6InUvenBESVBzOCtUSlZUaW5tYVBaUjBqd2xyd2lJU1lSRVJ1ZnphNUVzS2s9IiwidmVyIjoiMi4wLjguNjIiLCJuYmYiOjE1MTIwNzEyODV9.4y__KpTFSgkdD_dMAuMiClbSfcmnvdtr0IEoVHFB_Fw","sessionTimeoutSeconds":1800,"profileRequirements":null}
    

    Output

    Output below is capture group 1

    eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNVE8iLCJpYXQiOiIxNTEyMDcxNTg1Iiwic3ViIjoiOTEzNjQ0NCIsInV0ayI6InUvenBESVBzOCtUSlZUaW5tYVBaUjBqd2xyd2lJU1lSRVJ1ZnphNUVzS2s9IiwidmVyIjoiMi4wLjguNjIiLCJuYmYiOjE1MTIwNzEyODV9.4y__KpTFSgkdD_dMAuMiClbSfcmnvdtr0IEoVHFB_Fw
    

    Explanation

    • "token" Match this literally
    • \s* Match any number of whitespace characters
    • : Match this literally
    • \s* Match any number of whitespace characters
    • " Match this literally
    • ([^"]*) Capture any character except " into capture group 1
    • " Match this literallly