简体   繁体   English

通过JMeter正则表达式提取器从响应中提取ID

[英]Extract ID from the response through JMeter regular expression exractor

I need to extract the session id and user id from the response through jmeter using regex. 我需要使用正则表达式通过jmeter从响应中提取会话ID和用户ID。

Response: 响应:

\"SessionIdEncr\":\"TLGdqhQbxkEOziVJYXMD7Q==\",\"userIDEncr\":\"9xJbu+CGYN5RnnALeC8mKQ==\"

What is the regex for it? 正则表达式是什么?

Try using this pattern: 尝试使用以下模式:

"SessionIdEncr":"([^"]+)","userIDEncr":"([^"]+)"

Not much to explain about the regex pattern, other than perhaps that ([^"]+) uses a trick to capture the values. It proceeds only so long as it doesn't capture another double quote, which would mean the closing quote for that key/value pair. 关于正则表达式模式的解释不多,除了([^"]+)使用技巧来捕获值外,它仅在不捕获另一个双引号的情况下才会继续进行,这将意味着该键/值对。

This would capture the base 64 encoded session ID and user ID, and would make them available in the extractor template as $1$ and $2$ . 这将捕获基数为64的编码的会话ID和用户ID,并使它们在提取器模板中可用为$1$$2$

Demo 演示版

  1. Add Regular Expression Extractor as a child of the request which returns this text 正则表达式提取器添加为返回此文本的请求的子代
  2. Configure it like: 像这样配置:

    • Name of created variable: anything meaningful, ie id 创建的变量的名称:任何有意义的变量,即id
    • Regular Expression: \\\\"SessionIdEncr\\\\":\\\\"(.+?)",\\\\"userIDEncr\\\\":\\\\"(.+?)" 正则表达式: \\\\"SessionIdEncr\\\\":\\\\"(.+?)",\\\\"userIDEncr\\\\":\\\\"(.+?)"

      Other fields can be left blank 其他字段可以留空

      JMeter正则表达式提取2个变量

  3. You will be able to refer SessionID as ${id_g1} and UserId as ${id_g2} where required 您将可以在需要时将SessionID称为${id_g1} ,将UserId称为${id_g2}

    JMeter Regex进行了数场比赛

References: 参考文献:

In JMeter you can test/fix your regex using View Results Tree's Regexp Tester : 在JMeter中,您可以使用View Results Tree的Regexp Tester测试/修复正则表达式:

The Regexp Tester view only works for text responses. Regexp Tester视图仅适用于文本响应。 It shows the plain text in the upper panel. 它在上部面板中显示纯文本。 The "Test" button allows the user to apply the Regular Expression to the upper panel and the results will be displayed in the lower panel. “测试”按钮允许用户将正则表达式应用到上部面板,结果将显示在下部面板中。 The regular expression engine is the same as that used in the Regular Expression Extractor. 正则表达式引擎与正则表达式提取器中使用的引擎相同。

In your case create 2 Regular Expression Extractor s, for session ID use the following Regular Expression: 在您的情况下,创建2个正则表达式提取器 ,对于会话ID,请使用以下正则表达式:

SessionIdEncr\\":\\"([^\\]+)

Get the User ID similarly. 同样获得用户ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM