How to get HTTP POST request body in Beanshell Preprocessor? - jmeter

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.

Related

How to send complete form-urlencoded post data from preprocessor

I know we can send/initialise the post data parameter variables from preprocessor but my requirement is i want to send complete post data/pay load which is shown in my screenshot from jsr223 preprocessor.
You have sampler shorthand which stands for HTTPSamplerProxy in the JSR223 PreProcessor
There is HTTPArgument class where you can specify name, value and whether they're encoded already or JMeter should perform the URL-encoding itself
So for each parameter you want to pass you need to add the line like:
sampler.getArguments().addArgument(new org.apache.jmeter.protocol.http.util.HTTPArgument('parameter_name', 'parameter_value', false))
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

Jmeter - passing JSON response array to the next graphql http request

I have 2 graphql http requests as following.
The first http request's response returning json like so:
{"data":{"customers":{"customerIds":["0e1c7b05-79c6-40c6-9144-7230a836fe04", "45677b05-79tt-56c6-9144-7230a836bbbb"]}}}
I then created the json extractor and the beanshell to save the customer ids in the property
The 2nd Http request then using the above customerids property as part of the graphql request body
but I;m getting the 500 InternalServerError due to the quotes in the list customerids below not being escaped. how can i escape them ?
POST data:
{"query":"query getCustomerInfo {\n getCustomerInfo(customerIds: \"["0e1c7b05-79c6-40c6-9144-7230a836fe04"]\") {\n firstName\n lastName\n school\n }\n}"}
Don't inline JMeter Functions or Variables into your scripts
When using this feature, ensure your script code does not use JMeter variables directly in script code as caching would only cache first replacement. Instead use script parameters.
Don't use Beanshell, since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting
So replace your Beanshell Assertion with the JSR223 PostProcessor and use the following code:
props.put("customerIds",vars.get("customerIds").replaceAll("\"","\\\\\""));
This way you will get the following customerIds property value:
[\"0e1c7b05-79c6-40c6-9144-7230a836fe04\",\"45677b05-79tt-56c6-9144-7230a836bbbb\"]

How can I dynamically post Request body (xml) and validate the response (xml)?

Is there a way to send the XML request dynamically and validate the XML response?
My scenario is:
I will have a CSV dataset config and inside the csv file I will have two column, the first one is for the inputXMLFilePath and the second column is the expectedXMLResposneFilePath.
So I need to have a JSR233 PreProcessor under HTTP request sampler, read the input file path convert it to the post body, and also has another JSR233 sampler for load the expected response from the expectedXMLResponseFilePath and compare it with the previous XML response. I have a snippet for JSON which is working fine. but for XML how can I do it?
You can use __FileToString() function for both use cases:
To send the XML request body, like ${__FileToString(${inputXMLFilePath},,)} (where ${inputXMLFilePath} is the variable from the CSV Data Set Config)
To validate the response using Response Assertion configured like:
Field to Test: Text Response
Pattern Matching Rules: Equals
Patterns to test: ${__FileToString(${expectedXMLResponseFilePath},,)}
You can use JMeter Functions literally at the any place of your Test Plan so their flexibility is higher than for other test elements. Also JMeter Functions are being compiled into native Java code therefore their execution speed will be higher and footprint will be less comparing to Groovy scripting.
Check out Apache JMeter Functions - An Introduction article to learn more about JMeter Functions concept.

How to get Sampler Body data from beanshell Pre-Processor - JMeter

I have Http Sampler body as follows,
{"Uname":"admin","Signature":"${Sign}","LoginTime":"${LogTime}","Action":"Do_Action"}
I have to get the value of "Action" from the above body, and that value nned to be sent to Pre-processor which will be useful to do further action.
Help me out of this...!!
Thanks!
I would recommend switching to JSR223 PreProcessor and Groovy language as:
Groovy has built-in JSON support
Groovy performs much better than Beanshell
Example Groovy code to extract "Action" from the request body and store it into ${action} JMeter Variable will look like:
def body = new groovy.json.JsonSlurper().parseText(sampler.getArguments().getArgument(0).getValue())
vars.put('action', body.Action)
See Groovy is the New Black article for more details.

Parameterize tag value inside XML contained in the parameter at runtime in JMeter

I saved the whole XML in a DB table and am fetching the XML in JDBC sampler and using it in HTTP sampler. I want to parameterize a value inside a particular tag value inside this fetched xml at runtime. Could someone tell me how to do that. Thanks in advance
In http sampler ==> post body
xmldoc = ${xmlfromdb}
Here am able to fetch the whole XML and I can submit it successfully. How to parameterize a tag value inside this fetched xml at runtime.
You can do it via scripting like:
Add Beanshell PreProcessor as a child of the HTTP Request sampler
Put the following code into the PreProcessor's "Script" area
String body = sampler.getArguments().getArgument(0).getValue();
body = body.replace("Original Tag Value", "New Tag Value");
sampler.getArguments().removeArgument(0);
sampler.addNonEncodedArgument("", body, "");
sampler is a pre-defined variable available for Beanshell (and some other Test Elements) which stands for parent Sampler instance and in case of HTTP Request it's HTTPSamplerProxy. See JavaDoc for more information on available methods and fields and How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on using JMeter and Java API from Beanshell test elements inside JMeter script.

Resources