I want to send the data that has been url encoding in Jmeter - jmeter

I want to send a data that has been encryption.
So, I used JSR223 Sampler.
---skip---
def encrypted = Crypto.encrypt("AES/CBC/PKCS5Padding", "{"MY_SENTENCE"}", Crypto.generateSecretKey(), Crypto.generateIv())
vars.put("enc_data", encrypted);
Body Data in HTTP Request.
{ "data": "${enc_data}" }
In Results Tree
Request Body data was not url-encoding.
I want to send a data of url encoding, what should I do?
I don't know that.
I wrote Body Data in HTTP Request. So, I can't click the Parameters.
And I added Content encoding (UTF-8) it was not working too.

Either use __urlencode() function in the Body Data tab directly:
{ "data": "${__urlencode(${enc_data})}" }
JMeter Functions can be placed anywhere in the test plan and they're evaluated in the place where they're present so your ${enc_data} variable will be url-encoded in the runtime. See Apache JMeter Functions - An Introduction article for more information on JMeter Functions concept.
or call URLEncoder.encode() function in your Groovy script directly:
vars.put("enc_data", URLEncoder.encode(encrypted,"UTF-8"));

Related

JMeter - Unable to click Parameter tab when added Body Data

Why I cannot click on Parameters tab in HTTP Request section after I insert JSON body data in Body Data tab? I tried to define correlation and of course this will require us to define the variable as well. Just wondering, after I'm done with correlation part, how can I insert the variable in Parameters if it is disabled?
You can't send Parameters and Body Data, you must choose only one option of sending request body
If you need to send extra parameters in additional to request body then use query parameters in Path field, for example
path?parameter1=value&parameter2=value2
If you want to variablize/correlate the JSON request body - you can put the relevant JMeter Functions/Variables directly into "Body Data" tab
as you can see all the functions/variables are getting replaced by their respective values.
Also most probably you're getting JSON as the response so it makes sense to consider switching to JSON Extractor or even better JSON JMESPath Extractor for the correlation.

Is it possible to parameterize Post requests payload and then encode it to base64 in Jmeter

The background is that I want to make a REST to Google pub/sub. Where the data need to follow this format:
Link to documentation of Pub/Sub format
My current solution is that I uses an BeanShell PreProcessor script to encode the payload to base64 before I send the request to the endpoint. This solution works, but I would like to
parameterize the data in the payload, instead of having the whole payload inserted as test data in the csv-file.
BeanShell PreProcessor used to encode the message before it is being sent:
import org.apache.jmeter.protocol.http.util.Base64Encoder;
String csv_payload = vars.get("csv_payload");
String csv_payload_encoded = Base64Encoder.encode(csv_payload);
vars.put("csv_payload_encoded", csv_payload_encoded);
Payload populated from the csv-file in Post request:
{
"messages": [
{
"data":"${csv_payload_encoded}",
}
]
}
Example of payload data stored in the csv-file that is sent in the request:
{"identId":"123456","requestId":null,"payload":{"header":{"requestid":1,"timeStamp":1617873956,"version":"0.0.0.1","eventId":0001,"creatorId":0,"messageTTL":34560},"body":{"checkid":001,"checkData":{"diagnosticsData":{"troubleSource":0,"data":"[2020-01-01 16:00:53.707961][[lat[0]][long[0]][alt[0]][canbetrust[0]][mars[0]]][signal[5]][TEM2 wake up]"}}}}}
Example of the encoded payload that the request sends to google pub/sub:
{
"messages": [
{
"data":"eyJpZGVudElkIjoiMTIzNDU2IiwicmVxdWVzdElkIjpudWxsLCJwYXlsb2FkIjp7ImhlYWRlciI6eyJyZXF1ZXN0aWQiOjEsInRpbWVTdGFtcCI6MTYxNzg3Mzk1NiwidmVyc2lvbiI6IjAuMC4wLjEiLCJldmVudElkIjowMDAxLCJjcmVhdG9ySWQiOjAsIm1lc3NhZ2VUVEwiOjM0NTYwfSwiYm9keSI6eyJjaGVja2lkIjowMDEsImNoZWNrRGF0YSI6eyJkaWFnbm9zdGljc0RhdGEiOnsidHJvdWJsZVNvdXJjZSI6MCwiZGF0YSI6IlsyMDIwLTAxLTAxIDE2OjAwOjUzLjcwNzk2MV1bW2xhdFswXV1bbG9uZ1swXV1bYWx0WzBdXVtjYW5iZXRydXN0WzBdXVttYXJzWzBdXV1bc2lnbmFsWzVdXVtURU0yIHdha2UgdXBdIn19fX19",
}
]
}
If there are any feedback I would appreciate it or any other improvements suggestion
for how I would be able to proceed in order to parameterize and encode the payload to base64 eg:
Example of
Don't use Beanshell, since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting
There is __base64Encode() function available via Custom JMeter Functions plugin bundle installable using JMeter Plugins Manager
If you CSV file has references to other JMeter Functions or Variables you can resolve them by wrapping into __eval() function like:
to read the file:
${__FileToString(somefile.csv,,)}
to read the file and resolve any functions or variables in it:
${__eval( ${__FileToString(somefile.csv,,)})
to read the file, resolve any functions or variables and Base64-encode the result:
${__base64Encode(${__eval( ${__FileToString(somefile.csv,,)})},)}
Demo:

How to get URL having ".../?where={'key1' : 'value1' , 'key2' : 'value2':.........}" in Jmeter http request

I need to load test one Python api in below URL format:
//IP:PORT/Sub_Part/?where={"KEY1":"VALUE1","KEY2":"VALUE2","KEY3":"VALUE3"}
I tried to pass the key value pair through csv as well as directly in http request but getting error message.
java.net.URISyntaxException: Illegal character in query at index 47:
http://IP:PORT/Sub_Part/?where={"KEY1":"VALUE1","KEY2":"VALUE2","KEY3":"VALUE3"}
Here key and value are dummy data I have placed here for easy understanding.
Please help me with correct syntax for this URL.
Thanks in advance for all your help.
In REST APIs, JSON objects are typically sent (POST) or returned in the body of the request. They are not typically encoded as part of the URL.
For a GET request, you can either pass the information as segments in the url or as querystring parameters.
For more details refer here, How to send a GET request with a "/" in the query
As per HTML URL Encoding Reference:
URLs can only be sent over the Internet using the ASCII character-set.
so you need to define the request in JMeter's HTTP Request sampler as follows:
Pay attention to URL Encode? checkbox, it has to be ticked otherwise the parameter will be sent "as is"
Another option is using __urlencode() function like:
http://IP:PORT/Sub_Part/?where=${__urlencode({"KEY1":"VALUE1"\,"KEY2":"VALUE2"\,"KEY3":"VALUE3"})}
which will generate an encoded sequence which you could use in the URL path:
%7B%22KEY1%22%3A%22VALUE1%22%2C%22KEY2%22%3A%22VALUE2%22%2C%22KEY3%22%3A%22VALUE3%22%7D
as you can see, all non-ASCII characters where removed.
Check out Apache JMeter Functions - An Introduction to learn more about JMeter Functions concept.

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 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.

Resources