首页 > 解决方案 > JMeter: Is it possible to add HTTP Request sampler during run?

问题描述

I have an HTTP request that returns a JSON object that contains multiple urls (mostly image resources). I do not know the number of the urls that will be in the response JSON and that number may vary during time. I need to create an HTTP request for each of the urls that i received.

Is it possible to create HTTP Request samplers while running the JMeter flow? Is there a different solution one can advise me to do in order to send HTTP requests to the list of the urls?

Thanks

标签: jmeterjmeter-5.0

解决方案


  1. Add JSON Extractor as a child of the main request and come up with a JSON Path expression to extract links to images and whatever else URLs you would like to hit
  2. Add ForEach Controller and configure it to read the JMeter Variable reference name from step 1 and set output variable to anything meaningful
  3. Add HTTP Request sampler as a child of the ForEach Controller and configure it to use the output variable
  4. That's it, JMeter will trigger a HTTP Request sampler for each URL present in the original JSON

  • Given you have the following JSON:

    {
      "urls": [
        {
          "url": "http://example.com"
        },
        {
          "url": "http://jmeter.apache.org"
        },
        {
          "url": "http://jmeter-plugins.org"
        }
      ]
    }
    
  • You can extract the URLs into JMeter Variables using the following JSON Extractor setup:

    enter image description here

    it will give you the following JMeter Variables:

    url_1=http://example.com
    url_2=http://jmeter.apache.org
    url_3=http://jmeter-plugins.org
    url_matchNr=3
    

    enter image description here

  • now if you add ForEach Controller and configure it like:

    enter image description here

  • you will be able to refer the URLs as ${current_url} in the HTTP Request sampler which is the child of the ForEach Controller

    enter image description here


推荐阅读