0

I have recorded script using BlazeMeter plugin and I want to the use access token which I receive in successful login request, in another request. My Test plan looks like as below

Thread Group : [A]
      |- HTTP Sampler - Login Page
           |-Regular Expression Extractor [getToken]
      |-HTTP Sampler - Other Page
           |-Beanshell PreProcessor[Set Header in Authorization]
  1. Regular Expression Extractor parameters and values like below :

    Variable Name : token
    Regular Expression  : {“access_token”:”(.+?)"
    Template : $1$
    Match No. : 0
    
  2. Beanshell PreProcessor script like below

    import org.apache.jmeter.protocol.http.control.Header;
    log.info("Start");
    sampler.getHeaderManager().add(new Header("Authorization","Bearer"+vars.get("token")));
    log.info(vars.get("token"));
    
Amol Chavan
  • 3,835
  • 1
  • 21
  • 32

2 Answers2

0

Could you add debug sampler and try first to confirm your regular expression extractor working as expected? It should provide you the required value of token.

If your token has the required value, I will suggest you to add HTTP Header Manager config element by right clicking on HTTP sampler

HTTP Request => Add => Config Element => HTTP Header Manager

In this config element, you can visually add the Headers as below: Sample image for HTTP Header Manager

Please Note That:- You have not provided any space/hyphen(-) or between keyword Bear and token.

Refer this link for details :- https://stackoverflow.com/a/24550552/1115090

Amol Chavan
  • 3,835
  • 1
  • 21
  • 32
0
  1. Most probably your Regular Expression Extractor fails as your quotation marks look utterly suspicious. You can double check if the token variable really has the anticipated value using Debug Sampler and View Results Tree listener combination. Also check out jmeter.log file for any suspicious entries, if your Beanshell script fails - the cause will be printed there.
  2. The response data of the Login Page seems to be JSON therefore it makes sense to use the JSON Extractor instead of the Regular Expression Extractor. It allows using JSON Path language in order to extract "interesting" bits of data from the responses. In your case the relevant JSON Path expression would be $.access_token

  3. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so consider migrating to the JSR223 PreProcessor and Groovy language (you can re-use the same code)

  4. You don't even need the scripting, you can add Authorization header (as well as any other header) using HTTP Header Manager
Dmitri T
  • 159,985
  • 5
  • 83
  • 133