ArrchanaMohan
ArrchanaMohan

Reputation: 2576

How to call the javascript variable in JSON header for JMETER

I just have the "JSR223 postprocessor" in Jmeter with below script

var context_data = {"referralId":${referral_id}};
console.log(context_data);
const secret = CryptoJS.enc.Utf8.parse(JSON.stringify(context_data))
console.log(secret);
const encoded_referral_id = CryptoJS.enc.Base64.stringify(secret);
console.log(encoded_referral_id);

The above script I need to get "encoded_referral_id" value and need to pass another input request header section.

Can someone help me how can I get the "encoded_referral_id" and pass it to another subsequence API header.

Upvotes: 0

Views: 738

Answers (2)

Dmitri T
Dmitri T

Reputation: 168157

  1. Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting, using JavaScript is some form of a performance anti-pattern so consider migrating to Groovy language
  2. Never inline JMeter Functions or Variables into JSR223 scripts
  3. All your code does is Base64 encoding so you can use __base64Encode() function which will be even more easy and fast
  • Example implementation using __groovy() function:

     ${__groovy(('{"referralId":' + vars.get('referral_id') + '}').bytes.encodeBase64().toString(),)}
    
  • Example implementation using __base64Encode() function:

     ${__base64Encode({"referralId":${referral_id}},)}
    

Demo:

enter image description here

More information: Apache Groovy - Why and How You Should Use It

Upvotes: 2

Janesh Kodikara
Janesh Kodikara

Reputation: 1841

You could create a variable within the JSR223 PostProcessor and read the value of the variable in subsequent components including header.

Steps 1 : Creating the variable encoded_referral_id

vars.put("ENCODED_REFERRAL_ID", encoded_referral_id )

Step 2 : Reading value of the variable

${ENCODED_REFERRAL_ID}

JMeter Variables API documentation could be useful to get an idea of available methods for manipulating the variables.

Upvotes: -1

Related Questions