简体   繁体   中英

how to take part of a dynamic http request and encrypt it and replace the result inside the http request using Jmeter

I'm new to jmeter. I have a dynamic http request where certain values depends on the values of the previous http request. There is a encryptedKey and value as well. This value is calculated based on the details object which is dynamic. I'm writing a java function to encrypt part of the dynamic request. The encryptedValue has to be replaced by the result of the java function. Dynamic Http Request :-

{
  "ver": "1.0",
  "timestamp":"2019-08-28T11:39:57.153Z",
  "Details": {
        "key": "Previous API call value",
        "key": "Previous API call value"
      },
  "EncryptedKey": "EncryptedValue"
}

I need to take only Details object and apply the encrypt util on it. Then I have to replace the "EncryptedValue" with the result of the java function and then make the http request. How do I do this in jmeter?

What I've tried so far :-

Currently I'm loading the details object into a separate file and reading it using java and encrypting it.

So my final http request is as follows :-

{
  "ver": "1.0",
  "timestamp":"2019-08-28T11:39:57.153Z",
  "Details": {
        "key": "Previous API call value",
        "key": "Previous API call value"
      },
  "EncryptedKey": "${__groovy(new com.util.Encryption().encryptData(), encryptedValue)}"
}

But that is not able to replace the "Previous API call value" with the result of the previous http request. Is there any other way to solve this?

If you want to fully replace the request body you can consider using JSR223 PreProcessor and use the code like:

Disclaimer: I cannot guarantee that below code will work as your sample request data is not valid JSON and I have no idea how your Encryption().encryptData() function is implemented

def requestBody = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
def key = requestBody.Details.key
requestBody.EncryptedKey = new com.util.Encryption().encryptData(key)
def arguments = new org.apache.jmeter.config.Arguments()
sampler.setArguments(arguments)
sampler.addNonEncodedArgument('', new groovy.json.JsonBuilder(requestBody).toPrettyString(),'')
sampler.setPostBodyRaw(true)

But you will have to re-write your com.util.Encryption().encryptData() function to take key as the parameter instead of reading it from the file system.

In the above code example sampler stands for HTTPSamplerProxy class, see JavaDoc for all available functions.

Also check out Apache Groovy: Parsing and producing JSON for more information about these JsonSlurper and JsonBuilder classes and Apache Groovy - Why and How You Should Use It article for comprehensive overview of Groovy scripting in JMeter in general

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM