Python requests – HTTP library. Examples

If you need to send some HTTP requests and that requires additional processing on your side maybe it’d be better to draft some script. If you happen to know python then you can try its Requests library. Some simple snippets that can be used are placed further.

Set up a session

import requests
from requests import Request, Session
from requests.auth import HTTPBasicAuth

s = requests.Session()

Send POST request:

registrationData = {'registrationCode' :registrationCode ,'customerReference': customerRefNo} + '/customer-account/register',headers = headers, data = registrationData)

Just add required headers e.g

headers = {
 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36'

Send POST with json data

authorisationPayload = {"personRoles":[{"role":"Owner","personId":userId}],
 ["Full permissions","Amend ","No access - cs","No access - cs agree"],
 "personId":userId}]} + '/organisation/' +str(businessID),headers = headers, json = authorisationPayload)

Dealing with Cross-site request forgery

If we have XSRF token added to requests we need to extract it.

def getXsrf(cookies):
    for cookie in s.cookies:
       if =='XSRF-TOKEN':
          return cookie.value

and add it to our headers

xsrf = getXsrf(s.cookies)
 headers['X-XSRF-TOKEN'] = xsrf

Extract data from Json response:

 if ( ra.status_code == 200 ):
   organisationID = ra.json()['_data'][0]['id'] 

In case of unverified HTTPS requests we can suppress warnings (if we really want it) :

from requests.packages.urllib3.exceptions import InsecureRequestWarning

We also probably need to add to our request this parameter  verify=False
to skip verification of HTTPS certificate. Use it if needed of course.

Python requests – HTTP library. Examples

Jmeter – If controller example

If controller  example to control which request to send based on response of the other sampler.

We’re using Dummy Sampler to generate requests.

We also need to extract response’s code. We can use Regular Expression Extractor to get it:

  • Field to check: Response Code
  • Reference Name: RESPONSE_CODE
  • Regular Expression: (\d+)
  • Template $1$

First If Controller Condition

  • ${RESPONSE_CODE}!="200"

Second If Controller Condition

  •  ${RESPONSE_CODE}=="200"

Setup Dummy Sampler to return code 200.

Dummy Sampler - 200


if controller - yes condition

And now edit it to return something other than 200

Dummy Sampler - 404


If Controller result


Jmeter – If controller example

Jmeter’s Variables From CSV File – relative path

Jmeters Variables From CSV File config element set its base directory as a user’s home directory. If you ‘Browse’ your file it will put something like:


So in such case we need to pass base directory to be able to work with relative paths for our files.

1. Define base directory in User Defined Variables:

baseDir =>  ${__BeanShell(import; FileServer.getFileServer().getBaseDir())}

2. Use it in your element:

Jmeter’s Variables From CSV File – relative path

Use Jmeter’s base directory

You can access these variables using Beanshell as follows:

  1. JMeter’s base dir
    String baseDir = FileServer.getFileServer().getBaseDir();
    vars.put("baseDir", baseDir);
  2. JMeter’s script location (GUI mode)
    import org.apache.jmeter.gui.GuiPackage;
    String testPlanFile = GuiPackage.getInstance().getTestPlanFile();
    vars.put("testPlanFile", testPlanFile);
  3. JMeter’s script name (non-GUI mode)
    String scriptName = FileServer.getFileServer().getScriptName();
    vars.put("scriptName", scriptName); 

If you prefer you can get the same using __Beanshell() function as

${__BeanShell(import; FileServer.getFileServer().getBaseDir())}

and it will return current base directory where it is called.


Use Jmeter’s base directory