HTTP Response Header Regex - regex

I had planned to use Jmeter Regex Extractor to get a Session ID in HTTP Response Header. This is the example of the HTTP Header :
HTTP/1.1 200 OK
x-powered-by: yoke
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST,GET,DELETE,PUT
Access-Control-Allow-Headers: X-Requested-With,jsessionid,Origin,Accept,Content-Type
Access-Control-Expose-Headers: Content-Size,Message,Total-Pages,Total-Count,Current-Page,jsessionid,Origin,Total-Outstanding,Content-Range
content-type: application/json
jsessionid: 10838d69-f9ac-4c70-b1f7-9447a7a6a463
Content-Length: 106
All I need to get is :
10838d69-f9ac-4c70-b1f7-9447a7a6a463
I use this REGEX :
jsessionid: [^\n]+
But I get :
jsessionid: 10838d69-f9ac-4c70-b1f7-9447a7a6a463
Can you help me with it?
Thank you
Best Regards,
Stefio

Use the regex expression
jsessionid: ([^\n]+)
and Template
$1$
Your issue has to do with regex grouping. Group 0 is the entire match, which is the default of Jmeter Regex Extractor. Group 1 is what was matched by the regex inside the first set of parenthesis. Template $1$ says to use the contents of group 1 as your result. Regex grouping can get much more complicated, so read tutorials if you want to grab multiple values from a regex expression.
Jmeter Regex Extractor user manual

Look into lookaround for regex, for you particular case it'd be lookbehind regex. This outght to work, untested though:
(?<=jsessionid:\s).+
The (?<=jsessionid:\s) part means literraly match jsessionid: but don't include it in results

JSESSIONID is basically a cookie so I don't think you need to extract it with regex.
I can think of 2 scenarios why you might need it:
You need to pass it with the next request as a header
You need its value for something else
In both cases you can use HTTP Cookie Manager
For the 1st option: it handles cookies automatically
For the 2nd option: given CookieManager.save.cookies property set to true you should be able to access the cookie value as ${COOKIE_JSESSIONID}

Related

JMeter Regular Expression Extractor to extract custom header "access-token"

I have bellow response header that is giving me the problem when extracted using Regular Expression Extractor. To keep to story short, after troubleshooting I come up with a solution that works but is not perfect.
The Headers
Response headers:
HTTP/1.1 200 OK
Vary: Origin, Accept-Encoding
Access-Control-Allow-Credentials: true
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
X-Download-Options: noopen
X-Content-Type-Options: nosniff
access-token: GAbnLmcDzT4j5INPkSGwdbQzZIIFIaJoy4wBnmNUF4NEgGB11IfzTAMdqXyxIhAZ
Access-Control-Expose-Headers: access-token
Content-Type: application/json; charset=utf-8
The Solution
Regular Expression : access-token: (.+?)\n
Template : $1$
Refer to below picture on why I think the solution is wrong.
Extracted token is represented by multiple variable as result_token, result_token_g, result_token_g0, result_token_g1:
The Question
What is the correct Regular expression and template to get only the token.
TIA!
UPDATE:
Bellow excerpt from Regular Expression Extractor Doc actually help me better understand this question.
If the match number is set to a negative number, then all the possible matches in the sampler data are processed. The variables are set as follows:
refName_matchNr - the number of matches found; could be 0
refName_n, where n = 1,2,3 etc - the strings as generated by the template
refName_n_gm, where m=0,1,2 - the groups for match n
refName - always set to the actual template value if a match is found, otherwise, the default value.
It seems to me, that you are getting it right; result-token_g1 contains the intended capture, and it is replicated via your template to result-token.
Don't worry about result-token_g0; it's supposed to show the complete match: capture AND context.
Use this:
Regular Expression : access-token: (.\w*)
Template: $1$
Match No: 1
DefaultValue: Not found

Ignoring URL if contains specified word in URL GET parameters

I'm working on script that would show potentially dangerous HTTP requests, but I don't know how to filter URI in HTTP request correctly. The idea is to look if any URL is contained in GET parameters, but ignore the URLs which are added to GET parameter with specified word (for example - GET parameter with name goto can contain any URL. So if there is starting line of request like this ...
GET /check/request?first=1&second=http://domain.tld/something&third=3 HTTP/1.1
... there must be match. In case we have other request's starting line like ...
GET /check/request?goto=http://domain.tld/something HTTP/1.1
... this one should be ignored.
Base regex which matches any line with URL is:
^(GET|POST).*\?.*\=http\:\/\/.* HTTP\/.*$
I was trying to modify it correctly, but my version only matches lines which contains word goto in URL itself, not as parameter:
^(GET|POST).*\?.*(?!.*goto)\=http\:\/\/.* HTTP\/.*$
Any help would be appreciated.
UPDATE
^(GET|POST).*\?.*(?<!goto)\=http\:\/\/.* HTTP\/.*$
Check here
You probably meant lookbehind to http://.* rather than lookahead to .*:
^(GET|POST).*\?.*(?<!goto)\=http\:\/\/
Please see an example on regex101.

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.

Extracting from Response Header with Regular Expression

I'm trying to extract the confirmation number at the end of the location tag in the response header to a page using RegEx. The response header is as follows:
HTTP/1.1 302 Moved Temporarily
Date: Mon, 09 Sep 2013 17:55:50 GMT
Server: Apache-Coyote/1.1
Location: http://test.regtest.com/cart/confirmation?confirmationNumber=00284031
Content-Language: en
Content-Length: 0
X-Cnection: close
Content-Type: text/plain; charset=UTF-8
Vary: Accept-Encoding
For instance, if in the header the line is this:
Location: http://test.regtest.com/cart/confirmation?confirmationNumber=00284031
I am looking to return this to use as a variable later:
00284031
My current RegEx expression is something like this:
Location: http://test.regtest.com/cart/confirmation?confirmationNumber=(\d+)?
I am new to RegEx and what I wrote above is based off the example at the following link:
http://www.sourcepole.ch/2011/1/4/extracting-text-from-a-page-and-using-it-somewhere-else-in-jmeter
I need this confirmation number for a dynamic page redirect for a Jmeter script I am writing. Any help would be greatly appreciated and if you require additional information to help answer the question let me know!
Many thanks in advance.
Try this: Location: [\S]+?confirmationNumber=(\d+)
Your issue is the use of special characters in the string without escaping them - e.g.: ? and /
Note my ? is not matching the question mark in front of confirmationNumber, but instead is making the [\S]+ non-greedy.
If you want to be explicit, your version should work if modified like this to escape the characters with special meaning:
Location: http:\/\/test.regtest.com\/cart\/confirmation\?confirmationNumber=(\d+)?
You don't need to match the entire line to get the confirmation number, instead you can just match the number like this:
(?<=confirmationNumber=)(\d+)
(?<=confirmationNumber=) is called a look behind, what the expression says is to match one more digits (\d+) and put them into a group, only if those digits are preceded by the following string confirmationNumber=.
Rege101 Demo
Regexp as following
confirmationNumber=([0-9]+)

Regex HTTP header parsing

I'm trying to use Regex to get a bit if HTTP header parsing done. I'd like to use groups to organize some of the information:
Let's say I have this:
Content-Disposition: form-data; name="item1"
I'd like the result of my regex to create two groups:
contentdisposition : form-data
name : item1
I've tried several methods, but I can't seem to figure out how to do this. If name= doesn't exist then only one group should be created, but the regex should not error out.
Any ideas?
/Content-Disposition: (.*?);(?: name="(.*?)")?/ might be what you're looking for. It uses an optional greedy quantifier to get the name unless that would cause the match to fail.