Extracting JSON Response using regular expression extractor in Jmeter - regex

I want to extract the key value from the JSON response received for a login web service and pass that key value to all other subsequent services.
Note: This key value is dynamic.
The JSON response looks like this:
{
"key":"jwtjWgwnqIVapQeDh47rkFaKV6E",
"username":"Tester",
"password":"xyz",
"servername":"123.45.6.789",
"mailpath":"mail/tmail5.nsf",
"pin":"xxxx",
"defaultAttachments":"OFF",
"sendMailsFor":"W-1",
"serverTime":"10/09/2012 20:02:57"
}
Please help me in writing the reqular expression to extract the same and to pass that key value to all other next web services.
Please let me know what should I write in the fields "Reference Name:", "Regular Expression:", "Template:", "Match No.:" and how to declare the "Reference Name" in the next web services.
Thanks!

If key appearance is unique in response you may simply use Regular Expression Extractor added to the HTTP Request which returns json response, with regex like following:
HTTP Request
Regular Expression Extractor
Reference Name: authKey
Regular Expression: "key":"(.+?)"
Template: $1$
Match No.: 1
and refer extracted value as ${authKey} in all the further requests/samplers across your test.
As well you can look into this for the same situation.
And please don't be lazy in future to look first a bit around: it can appear that answer to your question already exists here.

Related

How to define a Regular Expression in Jmeter?

my problem is that I need to define a regular expression in Jmeter in order to make a DELETE method in my page. The real problem is that I cannot delete a specific token with an ID.
I defined a regular Expression which his name is "uidprofesor" that refers to the token. This token, when I try to delete it manually and catch the request, I get the following code in the Raw Response Data:
{"headers":
{"Location":["/api/profesores/5c745065-3155-4ff7-ac65-7699a36f611c"],
"X-afppApp-alert":["afppApp.profesor.created"],
"X-afppApp-params":["5c745065-3155-4ff7-ac65-7699a36f611c"]},
"body":{"id":"5c745065-3155-4ff7-ac65-7699a36f611c",
"nif":"12345678Q",
"name":"jmeter_122",
"firstSurname":"jmeter_122",
"secondSurname":"",
"address":"",
"postalCode":"",
"telephone":"",
"mobile":"",
"email":"",
"dateInsert":"24/04/2019"
},
"statusCode":"CREATED",
"statusCodeValue":201
}
, where I assume that "5c745065-3155-4ff7-ac65-7699a36f611c" is the ID.
If I wanted to define "uidprofesor" to catch that ID what should I define in the regular erxpression?
I tried defining the regular expression like:
{"headers":"(.+?)"
{"headers":{"Location":"(.+?)"
{"Location":"(.+?)"
But nothing. It shows the Default Value: NOT_FOUND and does not delete the selected token.
I let you here some extra info:
regular expression ${uidprofesor} = {"headers":"(.+?)"
In the HTTP Request the Path is:
https://desa1eap7.asturias.es/afpp-back/${uidprofesor}
If I do the deletion manually, in Google Chrome Developer Tab, It returns:
Requested URL: https://desa1eap7.asturias.es/afpp-back/api/profesores
If you need more info just ask it. Thanks.
Use jMeter JSON Extractor: https://jmeter.apache.org/usermanual/component_reference.html#JSON_Extractor
It uses JsonPath https://goessner.net/articles/JsonPath/
$.headers.Location[0]
Check online here https://jsonpath.com/

Change Special Characters From Extracted Regex In Jmeter

I have extracted an API key and JMeter does not like the special characters with the extracted regex.
Below is an example
I used regular expression extractor to extract the data from the previous page.
The value extracted is: TEST|1TWO3-TEST
Error message
When using this value later I get the following message:
java.net.URISyntaxException: Illegal character in query at index 5: (URL+Regex)
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parseHierarchical(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
at java.net.URL.toURI(Unknown Source)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:286)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1146)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1135)
at org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:434)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:261)
at java.lang.Thread.run(Unknown Source)
Hard Coded Value
When hard coding the value it like it works fine: TEST%7C1TWO3-TEST
Any advice regards how I can get the regex to look like my hard coded value via regular expression extractor?
You need to url encode the parameter.
In HTTP Request , ensure you check "Encode ?" in the parameters table:
If the parameter is in path, you can move it in table for GET requests.
If you end up needing to code, then use JSR 223 Post Processor after your extractor, check Cache script and add if your regexp extractor creates varName for example:
import java.net.URLEncoder;
String value = URLEncoder.encode(vars["varName"], "UTF-8");
vars.put("ENCODE_VALUE", value);
You can then use the variable through ${ENCODE_VALUE}
Your error isn't in the regex but afterwords in how you are using the data. After the value is extracted using regex, make sure you encode the data like this:
String q = "TEST|1TWO3-TEST";
String url = URLEncoder.encode(q, "UTF-8");
Now you can use url for whatever operation you were doing. I've assumed that you have already extracted TEST|1TWO3-TEST into a variable.
Test
// Output: TEST%7C1TWO3-TEST
Just wrap your variable coming from the Regular Expression Extractor with urlEncode() function like:
${__urlencode(${foo})}
Demo:
Check out How to Use JMeter Functions articles series for more information on using functions in JMeter tests.
There are also few useful ones available via JMeter Plugins project.
Thanks for your replies, I did the following at it worked.
String var1 = vars.get("var1");
String newVar1 = var1.replaceAll("\\|","%7C");
vars.put("var1", newVar1);
Thanks

how to use regular expression/ beanshell post processor extractor for extracting data from response

I have this reponse in the View tree listener
"__v":0,"language":"en","jobTitle":"Executive","firstName":"wer","lastName":"wre","email":"fde+98989#gmail.com","phone":"+1
213-321-3232","_companyId":"5723erera8b2196fdaer9502b86dc7","accountStatus":"UNVERIFIED","role":"ADMIN","emailActivationSecret":"XD1EererreQyVLFn1IjJg7QQNcDz9RLZ1xrKnl84XI5Nb5","_id":"5723a8b21er96fwereeda9ewree502b86dc8","products":[],"created":"2016-04-29T18:32:18.700Z"}
Now, i need to extract the email, email activation secret and id in to this link:
webserver-wat:3001/activateAccount?email=fde%298989%40gmail.com&secret=XD1EererreQyVLFn1IjJg7QQNcDz9RLZ1xrKnl84XI5Nb5&companyId=5723a8b21er96fwereeda9ewree502b86dc8
And send a post request inorder to activate the link. Is there any way of doing it? Would really appreciate as this is hindering our automation process
I believe it would be easier for you to use JSON Path Extractor (available via JMeter Plugins) as this way you will be able to easily parse multiline data, use conditional selectors and JSON Path expressions look much more friendly and human-readable than regex.
For instance:
To extracting a single value, i.e. email use the following JSON Path Extractor configuration:
Destination Variable Name: anything meaningful, i.e. email
JSON Path Expression: $.email
Refer extracted value as ${email} where required
Repeat above steps (use separate JSON Path Extractor) for other dynamic variables
If you want everything in a single shot:
Destination Variable Name: any suitable variable name, i.e. param
JSON Path Expression: $.[email, emailActivationSecret, _id]
Refer extracted values as:
${param_1} - for "email"
${param_2} - for "emailActivationSecret"
${param_3} - for "_id"
References:
Using the XPath Extractor in JMeter (scroll down to "Parsing JSON")
JSONPath - XPath for JSON
JSONPath Online Evaluator
the regex:
/.*"email":"(.*?)".*emailActivationSecret":"(.*?)".*"_id":"(.*?)".*/
the replacement string:
"webserver-wat:3001/activateAccount?email=$1&secret=$2&companyId=$1"
demo

RequestVerificationToken is not idetifying/or getting assigned to variable by JMeter in a ASP MVC 5 Project

I am currently in a need of a Load test for a web-solution, which requires for a user authentication in Homepage (Welcome Page).
When I seek for the POST SignIn parameters which are passing along with the login credentials using FireBug in FireFox,
i found out that Password, TenantName, UserName, __RequestVerificationToken are the parameters which are passing along with their values for a successful login.
I needed to simulate this process using the JMETER.
Therefore i have made a HTTP REQUEST (Visit Login Page) to Navigate to the page using JMETER, which successfully work.
Within the HTTP REQUEST (Visit Login Page) I have added a Regular Expression Extractor, to Extract the token,
since it is necessary to pass the token along with the sign in.
Reference Name : REQUEST_VERIFICATION_TOKEN
Reg Expression : input name="__RequestVerificationToken" type="hidden" value="([A-Za-z0-9+=/-\ _]+?)"
Template : $1$
Match No. : 1
Default Value : (blank)
And I have added a separate HTTP Request (Login to Web) for signin along with the Parameters as follows;
Password : ${Password}
TenantName : ${TenantName}
Username : ${Username}
__RequestVerificationToken : ${REQUEST_VERIFICATION_TOKEN}
But When I Run it, under the View Results Tree,
the Response Data is generating as "The anti-forgery token could not be decrypted..."
When i check for the Request displays under View Result Tree,
it displays as
POST data:
Password=123456&TenantName=tenant&Username=admin&__RequestVerificationToken=%24%7BREQUEST_VERIFICATION_TOKEN%7D*
Where in the Request I realized the value for the RequestVerificationToken is not getting initialized.
I was searching for a solution through blogs for two days, where ultimately without a proper assistance I tend to post this question over here.
This is one reference link which is usefully for a extent, from which i used :
http://build-failed.blogspot.com/2012/07/load-testing-aspnet-mvc-part-3-jmeter.html?showComment=1417672985397#c6427302313332055578
Can someone assist me of Why the RequestVerificationToken is not extracting properly, or getting initialized to the variable?
( Ps : please be kind enough to assume that I'm a beginner in JMeter, to be honest, when you are providing your precious reply )
In Regular expression '-' has a meaning & it has to be escaped properly if it is expected in your __RequestVerificationToken value.
So, the correct regular expression would be
input name="__RequestVerificationToken" type="hidden" value="([A-Za-z0-9+=/\-\_]+?)"
I created a temp variable & verified above regular expression. It works fine. If it does not work for you, Please provide the HTTP response if possible.

Regular expression for validating url with parameters

I have been searching high and low for a solution to this, but to no avail. I am trying to prevent users from entering poorly formed URLs. Currently I have this regular expression in place:
^(http|https)\://.*$
This does a check to make sure the user is using http or https in the URL. However I need to go a step further and validate the structure of the URL.
For example this URL: http://mytest.com/?=test is clearly invalid as the parameter is not specified. All of the regular expressions that I've found on the web return valid when I use this URL.
I've been using this site to test the expressions that I've been finding.
Look I think the best solution for testing the URL as :
var url="http://mytest.com/?=test";
Make 2 steps :
1- test only URL as :
http://mytest.com/
use pattern :
var pattern1= "^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)([A-Za-z]){2,3}(\/)?";
2- split URL string by using pattern1 to get the URL query string and IF URL has Query string then make test on It again by using the following pattern :
var query=url.split(pattern1);
var q_str = query[1];
var pattern2 = "^(\?)?([0-9A-Za-z]+=[0-9A-Za-z]+(\&)?)+$";
Good Luck,
I believe the problem you are having comes from the fact that what is or is not a valid parameter from a query string is not universally defined. And specifically for your problem, the criteria for a valid query is still not well defined from your single example of what should fail.
To be precise, check this out RFC3986#3.4
Maybe you can make up a criteria for what should be an "acceptable" query string and from that you can get an answer. ;)