On using a CSV config with JSON data the quotes are read incorrectly for the key value pair - jmeter

I have a POST data from CSV used in excel
{"Name":"","Token":-1,"TimeScheduleToken":"1","AccessRule":[{"ObjectToken":"528","ObjectName":"Common_ wash_Room_Exit","RuleToken":"528","RuleType":2,"StartDate":null,"EndDate":null,"ArmingRightsId":null,"ControlModeId":null}]}
When performing a post-execution the JSON data in the request is not as same as from the CSV. Find the request in the image
Quotes given for the key value pair is doubled up and showed in the request. How to resolve this, kindly suggest

Vittal,
I have tried to reproduce your issue in JMeter 3.3 and noticed that its working fine. Please find below the settings that you need to do in 'CSV Data Config' element.
Moreover, I would recommend that when you are creating any csv file for the data then open the notepad and enter your data and then save it as .csv file to avoid any unnecessary elements/characters in the data.
You can also refer to the blog post to get more information on API load testing using JMeter: JMeter Load Testing Against APIs

I have no idea regarding how you're getting these double quotation marks, however here is how you can remove them in the runtime:
Add JSR223 PreProcessor as a child of the HTTP Request sampler
Put the following code into "Script" area:
def originalData = sampler.getArguments().getArgument(0).getValue()
def normalizedData = originalData.replaceAll("\"\"","\"")
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument("",normalizedData,"")
sampler.setPostBodyRaw(true)
That's it, the JSR223 PreProcessor will replace all occurrences of double quotation marks with single quotation marks.
sampler is a shorthand to parent sampler class implementation, in case of HTTP Request sampler it would be HTTPSamplerProxy, see class documentation for all available functions and properties.
See Apache Groovy - Why and How You Should Use It article to learn more about using Groovy scripting in JMeter tests.

Related

how to save whole response message in jmeter

I am receiving below response in jmeter-
[{"Status":"Failed","ErrorDesc":"Duplicate Transaction Id","Amount":"23","CorporateID":"aaa","StatusCode":"ERR0DUP","TransactionReferenceNumber":"1111"}]
I need to save this whole response message.
I tried by using listner,and using csv file as well but only b able to save response like - OK,true
Please help me to save whole response as it is.
You could use the Save the Response to a File Sampler with following configuration
Ensure "Don't add number to prefix" check box is not checked
Set the Minimum Length of sequence number (e.g 6)
You can try with following options for unique file names
Check the Add timestamp
Use ${__threadNum} and/or ${__threadGroupName} fields with file name
response-${__threadGroupName}-{__threadNum}.json
If you want to save response into a variable just use Boundary Extractor with empty left and right boundaries or Regular Expression Extractor and (?s)(^.*) regular expression, see The Boundary Extractor vs. the Regular Expression Extractor in JMeter article for more details to learn more about differences of these two guys.
Example setup:
in the above setup the whole response will be saved into a JMeter Variable and you will be able to refer it as ${response} where required
If you want to save response into a file - go for Save Responses to a file listener, add it as a child of the request which returns the response and configure it like:
the above configuration will store the whole response of the parent sampler into response.txt file in "bin" folder of your JMeter installation

How to correctly extract .pem key from request response with JSON extrator

I am using a jmeter JSON extractor for a JSON that looks like this
{"type":"rpc","tid":7,"action":"SecurityManager","method":"getAuthenticationKey","result":"-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAydpVbheWPx4ZMbxJ8yCm\ndcP2EaRZD2R4PUmuFhdDdvpxT\/so00\/22orFQMgw8hrgEZ07ISzarOlclchm7DtF\nzxUzjGon1d5OJ2\/61niT+bAyuykn7y63\/BEtGS3KsR9ez3Ds+JR04Tca\/ajUYAIo\nrtAdCuvQuWkk4ZmZWywa7n899KOndL8S3G0R9Bex5XwfXJoE2BC6Ww75gwkzANFX\nIqkTYeepIMai3B8H31VIW2aJXURbjgN4yrk4sOy5a5JqnPEeCPKJR3nCrZDZGG06\ncoq0swW8oegNI9SFsiIqpDQ6Fi4WqqH5EMNu6FrkF3HAqwwyGljnogGNdnkwajiu\nCQIDAQAB\n-----END PUBLIC KEY-----\n"}
I am trying to use that value (for example just show it)
log.info("${key}")
, but I get the error
o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script26.groovy: 8: expecting anything but ''\n''; got it anyway # line 8, column 39.
log.info("-----BEGIN PUBLIC KEY-----
Is there something i am not doing right ?
You should never use ${} in Groovy script in JMeter.
Instead do this:
log.info("Got key:{}", vars["key"]);
Provided your variable is named key
And this is how you would configure JSON Extractor:
Given you use JSR223 Test Elements already you don't need the JSON Extractor, the PEM key can be extracted and printed in one shot via JSR223 PostProcessor
Add JSR223 PostProcessor as a child of the request which returns the above JSON
Put the following code into "Script" area:
vars.put('key', new groovy.json.JsonSlurper().parse(prev.getResponseData()).result)
log.info(vars.get('key'))
Enjoy the printed variable in Log Viewer window
Of course you will be able to access it as ${key} in the other Test Elements
References:
Groovy: Parsing and Producing JSON
Apache Groovy - Why and How You Should Use It
Going forward please avoid using JMeter functions and/or variables in Groovy scripts as they conflict with Groovy GString Templates, may be resolved into something which cause compilation or runtime failure and non-compatible with Groovy caching of compiled scripts feature negatively impacting JMeter performance.

jmeter prev.getResponseDataAsString getting wrong return

I have a looping process that will extract some values from a web service (working) and loop through to pull all the information for each value (working).
I need to capture the whole return into a variable so I can modify it and post it back up later.
Screenshot:
When the "Baseline for ..." get kicks in, I get the proper response
But the "Get response" BeanShell PreProcessor is picking up old responses
Screenshot:
Given where my "Get response" object is, I would assume the:
vars.put("ResponceData", prev.getResponseDataAsString());
...would grab the response from "Baseline for ${ID} of site ${callSite}". Please help!
You are using wrong test element. Beanshell PreProcessor is being executed before request therefore it acts properly and returns response from the previous request instead of current one. You need to change it to the Beanshell PostProcessor and your code will start working as you expect.
It is recommended to avoid scripting where possible, if you need to save response data into a JMeter Variable you can do it using i.e. Regular Expression Extractor. According How to Extract Data From Files With JMeter article the relevant configuration will be something like:
Reference Name: ResponceData
Regular Expression: (?s)(^.*)
Template: $1$
If you face a JMeter limitation which cannot be worked around without using scripting make sure you are using the most performing scripting language, since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language

How to get HTTP POST request body in Beanshell Preprocessor?

I am facing some trouble using jmeter. This is my use case, I am using CSV data source parameters to construct a HTTP POST request, the request body is read from a CSV column
which contains some placeholders like ${source_id}
I want to replace these placeholders with jmeter parameters which I am initialising through a regex/json extractor(read from the response of last PUT request). I tried using the jmeter variable name in the CSV file but the variable values are not getting substituted. I guess I will have to use the beanshell pre-processor to modify the HTTP POST request body. Can anyone help with the methods I can use to get the HTTP POST request body.
Something like
String requestBody = sampler.getArguments().getArgument(0).getValue();
should help.
sampler is a shorthand to parent sampler class instance, in your case it will be HTTPSamplerProxy, see the JavaDoc for all available methods and fields.
I would recommend considering migration to JSR223 PreProcessor and Groovy language as it is much faster and less resources consuming than Beanshell. Valid Beanshell code will be valid Groovy code so you should be able to convert to JSR223 elements with no or minimal changes. See Groovy is the New Black article for more details.

Jmeter - values from Regular expression extractor is not getting written in the file

I have added a regular expression as below in order to extract the response value inside the xml.
p400="http://newman.services.premium.hellocorp.com">(.+?)</p400:newman></soapenv:Body>
the reference name is "output_xml"
I have added a simple data writer as well and added "output_xml" to the sample variables in Jmeter properties file also. Still I am not able to see the xml getting written in the file.
Please advice me on this. Thanks!
I'm not sure, that it's possible with simple file writer, try FlexibleFileWriter plugin http://jmeter-plugins.org/wiki/FlexibleFileWriter/?utm_source=jpgc&utm_medium=link&utm_campaign=FlexibleFileWriter
And have you checked, that this extractor works on your data?
Try to add Debug Postprocessor (ensure that "JMeter variables" is set true) and View Results Tree elements to you project and inspect variables.
The given xml formats
p400="http://newman.services.premium.hellocorp.com">(.+?)</p400:newman></soapenv:Body>
You need to update the following regex formats to extract the response values
Regular expression formats
http://newman.services.premium.hellocorp.com">(.+)</p

Resources