Mahaveer
Mahaveer

Reputation: 11

How to store the JSON request in a variable using preprocessor and those variable should pass it to subsequent request as base64 value in JMeter

My requirement is i need to store the whole Json request in a single variable(using preprocessor) and pass those variable name in request to get the expected response.

Step 1 : I created a JSR223 Preprocessor test element and placed the Json body. Using CSV config element, i am passing different values to this Json body request at every iteration.

JSR223 request image

Step 2: In http request, i am passing that variable name(ex :foo) as in a base64 function to convert that whole Json request as base64 value to get the expected response.Http request.

Could please someone explain how to achieve this scenario here. Please refer the attached screenshot for reference.

Upvotes: 1

Views: 1188

Answers (1)

Dmitri T
Dmitri T

Reputation: 168157

  1. i am passing different values to this Json body request at every iteration - no, you don't, as per JSR223 Sampler documentation:

    Or Use Script Text and check Cache compiled script if available property.

    When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.

  2. You cannot have "plain" JSON in JSR223 test elements, it will cause compilation failure, you need to convert it into a String first. Assuming point 1 you need to use vars shorthand for JMeterVariables class instance to read p_loanamount and other values and to store the JSON into foo JMeter Variable, so it would be something like:

    def payload = """{
          "value": """ + vars.get('p_loanamount') + """,
          "type": "string",
          "key": "consumer_load_calculator.loan_amount",
          "stepTitle": "Loan Information",
          "formattedValue": "€ 4,000",
          "label": "LOAN AMOUNT"
        }"""
    vars.put('foo', payload)
    
  3. The same applies to your __groovy() function which should look like:

    ${__groovy(vars.get('foo').bytes.encodeBase64().toString(),)}
    
  4. Don't post code as images

Upvotes: 1

Related Questions