How to save response in a variable in jmeter

我与影子孤独终老i 提交于 2020-01-01 04:52:09

问题


I am performing load testing on my server using jmeter. In one of my post requests, I receive a unique id in the response. I need to send this id as a parameter in the following post requests. I am new to jmeter and don't have any idea how to do this. Help would be really appreciated.


回答1:


If you need to store the whole response into a variable - take the following steps:

  1. Add Beanshell PostProcessor as a child of the request which returns response you're looking for
  2. Put the following line into the PostProcessor's "Script" area:

    vars.put("response", new String(data));
    
  3. Refer extracted value as ${response} where required

    See How to Use BeanShell: JMeter's Favorite Built-in Component guide to lean more about Beanshell scripting in JMeter


Alternatively you can do the same with the Regular Expression Extractor, in that case relevant configuration will be:

  • Reference Name: response
  • Regular Expression: (?s)(^.*)
  • Template: $1$

If you need a part of response, not the whole response you can amend Regular Expression according to your needs as per Regular Expressions chapter of JMeter's User Manual




回答2:


If you really need to store the whole response into a variable, do the following:

  1. Add JSR223 PostProcessor as a child of the request which returns response you're looking for

  1. Put the following line into the "Script" area:

vars.put("response", prev.getResponseDataAsString());

  1. Use then this response as ${response} where you need it

But you rarely need to use the whole response and you should avoid it for big , in this case it is much better to use the Extractor that suits your response format:

  • JSON Extractor for JSON
  • CSS/JQuery Extractor for HTML extraction
  • XPath Extractor for XML
  • Regular Expression Extractor for all of them or any textual format



回答3:


You can use JMeter's Post-Processor Regular Expression Extractor to extract the required value from response. Just Add this under the sampler whose response will contain the required value.

In Reg expression extractor, you will define the variable name (referenceName), RegularExpression, template etc. Later you can use the value in this variable. To learn how to use Reg expression extractor you can refer to following tutorial.

https://docs.blazemeter.com/customer/portal/articles/1743642-using-regex-regular-expression-extractor-with-jmeter



来源:https://stackoverflow.com/questions/34463412/how-to-save-response-in-a-variable-in-jmeter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!