Stefio Kurniadi
Stefio Kurniadi

Reputation: 89

HTTP Response Header 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

Upvotes: 0

Views: 3280

Answers (3)

Dmitri T
Dmitri T

Reputation: 168082

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:

  1. You need to pass it with the next request as a header
  2. 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}

Upvotes: 0

Chris Z
Chris Z

Reputation: 357

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

Upvotes: 1

gwillie
gwillie

Reputation: 1899

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

Upvotes: 1

Related Questions