Special (Meta) Characters while using Regular Expression Extractor in JMeter - regex

In a response : there is a string like this.
,"Software":"abcde",
and i want to select abcde
i write the regular Expression as ,"Software":"([^"]+)",
then i used this variable in a GET request and a POST request.
in GET request it works but in POST request doesnt.
when i look at the post request i see some % characters. but they are not included in abcde string.
in abcde string some special characters like + and / may be present. and they will not transmitted properly to the request.
Why do yo think so? And are there any solution for this?

Check the variable value using Debug Sampler and View Results Tree listener combination, if the variable contains these "special" characters and they should not be there - amend your regular expression accordingly
If your response is in JSON format it worth considering switching to JSON Extractor
With regards to these % and /, can it be connected with URL Encoding?

Related

Extract value from websocket response

Im getting this response from a websocket
a["MESSAGE\ndestination:/user/p0000450/queue/bets/place/status\ncontent-type:application/json\nsubscription:/queue/bets/place/status-1\nmessage-id:p0000450-79\ncontent-length:139\n\n{"id":"c00656b318","groupId":"113686950","status":"PLACED","code":"0","message":"BetSlip with No.113686950 has been accepted","context":{}}\u0000"]
I would like to extract the betslip no.
Cant seem to generate regex that will extract this? please assist
i am planning to use regex extractor and beanshell postprocessor to save the data to excel for all responses
From the provided info it's hard to figure out all the possible formats of the "betslip number", but try this:
\bBetSlip with No\.\s*(\d+)\b
It captures any number of digits before the "BetSlip with No."-string.
Demo: https://regex101.com/r/NKfVm4/1
The easiest solution would be just going for the Boundary Extractor, all you need to do is to provide "left" and "right" boundaries and JMeter will fetch everything in-between:
So if you give:
Left boundary as BetSlip with No.
and right boundary as has been accepted
you will get what you're looking for.
If you have to do this using Regular Expression Extractor you can go for something like:
BetSlip with No.(\d+) has been accepted
Where:
() - grouping
d - matches any digit
+ - repetition
\ - escape of meta character
More information: JMeter Regular Expressions

How to remove double quotes from my output value so that it can be passed in another api request?

I have written a regular expression for fetching a value from response data in jmeter, now I want to use it in another api as request but the value I am getting is in double quotes so if I am sending request I am getting
I want to send this "1636afe5-d103-45d1-8786-d13022d92e07" value which I am getting from regular expression extractor to be without quotes so that it can be sent to another request.
java.net.URISyntaxException: Illegal character in query at index 73: https://processing_dev.cazcreekdev.com/mailmerge/getprogress?mailmergeId="1636afe5-d103-45d1-8786-d13022d92e07"
at java.base/java.net.URI$Parser.fail(URI.java:2936)
at java.base/java.net.URI$Parser.checkChars(URI.java:3107)
at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3195)
at java.base/java.net.URI$Parser.parse(URI.java:3137)
at java.base/java.net.URI.<init>(URI.java:623)
at java.base/java.net.URL.toURI(URL.java:1048)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:535)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:67)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1282)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1271)
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:627)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:551)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:490)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257)
at java.base/java.lang.Thread.run(Thread.java:830)
**i 'm not a java dev but i think is
str='"1636afe5-d103-45d1-8786-d13022d92e07"';
str = str.replaceAll(Pattern.quote('"'), "");
or
str = str.replaceAll(Pattern.quote('"1636afe5-d103-45d1-8786-d13022d92e07"'), '1636afe5-d103-45d1-8786-d13022d92e07');
Most probably your regular expression extractor configuration is not correct, it should be possible to remove the quotes on regex level
However if removing quotation marks from a JMeter Variable is your strict requirement you can use JSR223 PostProcessor and the following code:
def before = vars.get('foo')
log.info('Before: ' + before)
def after = before.replaceAll('\"', '')
log.info('After: ' + after)
vars.put('foo', after)
Replace foo with JMeter Variable reference name you're using in your Regular Expression Extractor and it should do the trick for you.
Demo:
More information: Apache Groovy - Why and How You Should Use It

Jmeter parameter extraction

I am getting a result in my jmeter test that I don't understand:
I am trying to extract the "totalRunning" value from this Json response:
{"notifications":[],"taskNotificationInfo":{"totalRunning":0,"totalCompleted":0,"totalCompletedWithErrors":0,"totalFailed":0,"totalPending":0,"requestTime":1458628767436,"hasRecords":false}}
My regex is configured as following:
Reference Name: TotalRunning
Regular Expression: "totalRunning":"(.+?)"
Template: $1$
Match: 1
Default Value: 1
screen shot:
I keep getting the default value instead of "0" in this case.
Am I extracting it from the wrong place?
Any help would be appreciated.
There is no problem with your regex totalRunning":(.+?),"totalCompleted"
only you need to select radio button Body instead of Body As a Document
refer snapshot:-
Change your regular expression as below:
Regular Expression: "totalRunning":(\d+)
In the question "totalRunning":"(.+?)" is being used as regular expression. Since values of totalRunning is not surrounded by quotes. So none was being matched and default value is being picked.
Below regex can also be used:
"totalRunning":(.+?),
Change your "Field to check" to "Body" as "Body as a document" is for binary files like Word, Excel, PDF, etc. see How to Extract Data From Files With JMeter article for more details. JSON is a usual text so it should be treated as "normal" response.
Also there is a special Post Processor - JSON Path Extractor available via JMeter Plugins project. In case of complex JSON, multiple or conditional matches, etc. it might be better and easier to use it.
The relevant JSONPath expression will be: $..totalRunning[0]

Regex HTTP Response Body Message

I use a jmeter for REST testing.
I have made a HTTP Request, and this is the response data:
{"id":11,"name":"value","password":null,"status":"ACTIVE","lastIp":"0.0.0.0","lastLogin":null,"addedDate":1429090984000}
I need just the ID (which is 11) in
{"id":11,....
I use the REGEX below :
([0-9].+?)
It works perfectly but it will be a problem if my ID more than 2 digits. I need to change the REGEX to :
([0-9][0-9].+?)
Is there any dynamic REGEX for my problem. Thank you for your attention.
Regards,
Stefio
If you want any integer between {"id": and , use the following Regular Expression:
{"id":(\d+),
However the smarter way of dealing with JSON data could be JSON Path Extractor (available via JMeter Plugins), going forward this option can be much easier to use against complex JSON.
See Using the XPath Extractor in JMeter guide (scroll down to "Parsing JSON") to learn more on syntax and use cases.
I suggest using the following regular expression:
"id":([^,]*),
This will first find "id": and then look for anything that is not a comma until it finds a comma. Note the character grouping is only around the value of the ID.
This will work for ANY length ID.
Edit:
The same concept works for almost any JSON data, for example where the value is quoted:
"key":"([^"]*)"
That regular expression will extract the value from given key, as long as value is quoted and does not contain quotes. It first finds "key": and then matches anything that is not a quote until the next quote.
You can use the quantifier like this:
([0-9]{2,}.+?)
It will catch 2 or more digits, and then any symbol, 1 or more times. If you want to allow no other characters after the digits, use * instead of +:
([0-9]{2,}.*?)
Regex demo

Jmeter Regex to return token

I have a JMeter HTTP Request that returns
{
"Token" : "VwAMVWXTakkdffdkEj1I9IiTr8DlYa89fK4yimmQNWSitIY1qBb1Qbs1FU9CfZHWMMlTed3hHOaBD7vJGNh9ZugFZuANtAomk17vIjg3Zgl1Fp0kulb6UTsbnkyyGNwNMGR"
}
in the response data. The string after the colon will change each time. I need this string to then be passed to another HTTP request. I have the rest set up but I am struggling with the regex, I get the default constantly.
Currently the regex looks like -
"Token":"(.+?)"
but doesn't work.
Can anyone help?
Thanks
Use a positive lookbehind,
(?<=\"Token\" : ).*
It matches all the characters which are just after to the string "Token" :
DEMO
OR
(?<=\"Token\"\s:\s\")[^\"]*
If you want the strings inside double quotes then use above regex.
DEMO
Below regex would capture the matched characters,
(?<=\"Token\"\s:\s\")([^\"]*)
Your return is JSON data, so the best option would be to handle it as JSON, which also would make your application far easier to maintain and evolve, when the data you are handling gets more complicated than just one attribute. Plus, you can handle it easily in JavaScript. Suggested reads:
Jmeter extracting fields/parsing JSON response
Use BSF Postprocessor to parse JSON response and save the properties as JMeter variables
Your regular expression needs to account for whitespace. I'd recommend using Regular Expression Extractor, which will make this alot easier for you.
Reference Name: FOO
Regular Expression: "Token" : "(.+?)"
Template: $1$
Use corresponding variable to access the match. ${FOO}
The variables are set as follows:
FOO_matchNr - Number of matches found, possibly 0
FOO_n - (n = 1, 2, etc..) Generated by the template
FOO_n_gm - (m = 0, 1, 2) Groups for the match (n)
FOO - By itself it is always set to the default value
FOO_gn - Not set at all
You can use regex ([^"]+) when you have received response from HTTP request.
Example:
"Token":([^"]+) --> Not required to add double quotation.