How to dynamically set the HTTP Referer header in JMeter?

15,852

Solution 1

Jmeter will read the HTTP Request Defaults only if no value is provided in the HTTP Request. If you leave the domain blank in the request, it will read from the request; else it use the local value.

As a quick fix, open your JMX file in a text editor and replace all instance of the domain with a blank value. Once you open your file back in Jmeter, add the domain value back into your HTTP Request Default config element.

To avoid this in the future, fill out the Request Default values BEFORE recording. Jmeter will automatically filter out values that match the default.

You can also do this with variables and user defined values. Be careful what default values you set. For example, if you said customerID = 123, any time Jmeter detects '123' while recording, it will replace it with ${customerID}. So, if there is the value '12345', Jmeter will record ${customerID}45

Solution 2

  1. Set JMeter variable named server at root of test plan
  2. Use that variable in HTTP request defaults Server Name field
  3. Use that variable in HTTP Header Manager Referer field setting

Also you may use JMeter property instead of variable if you want to pass it from command line to non-GUI JMeter run

Share:
15,852
tresstylez
Author by

tresstylez

I'm ready.

Updated on June 04, 2022

Comments

  • tresstylez
    tresstylez almost 2 years

    Hopefully this is a common issue with JMeter testing.

    I setup a Proxy Server via JMeter to 'record' a test case in our DEVELOPMENT environment. That worked fine and I setup some 'HTTP Request Defaults' to globally set the default 'Server Name or IP' and 'Protocol'.

    Now that the tests are completed on DEV, we have moved our code to our QA servers for more testing -- and I thought I would simply have to change the 'Server Name' in my defaults and then run the tests.

    One thing I noticed is that for each sampler in my test case, other headers are present in the 'HTTP Header Manager' for that specific sampler -- like User-Agent, Cache-Control, etc. The Referer header is the only one that concerns me because it still is 'hard coded' to be the DEV server.

    Is there a way to set this dynamically -- so that I can run this recorded test in multiple environments? How is this normally handled?

    Any help is appreciated! Thanks.

    jg

  • tresstylez
    tresstylez over 12 years
    Ah, okay -- this is more along the lines of what I'm talking about. So, quick fix is to search/replace in the JMX. Great. But, say in the test case I have a referer value of domain1.com/mypath?myvariable=myvalue, how can I setup the defaults to be domain2.com/mypath?myvariable=myvalue, when running the tests for domain2? Can you give me an example?
  • dinhlam
    dinhlam over 12 years
    Ah, good question. I have two HTTP Default Request elements - one for domain1, and one for domain2 and I toggle between the two by disabling the one I'm not using. (Technically I use "User Defined Variables" so I can have other elements - admin username/password also change between servers)
  • tresstylez
    tresstylez over 12 years
    Awesome. User Defined Variables, and multiple HTTP Default Request elements is what I needed. Thank you sir.