Advanced Payload and File Handling

Utilize the $read utility to design dynamic load patterns with external data.

1. Read a CSV File and Use Its Items

name: ReadCSVExample
variables:
- name: csvData
  value: $read(path=../data/sample.csv)
  as: csv
rounds:
- name: ReadCSVRound
  numberOfClients: 5
  arrivalDelay: 3000
  iterations:
  - name: UseCSVItem
    httpRequest:
      url: https://api.example.com/resource/${csvData[0,0]}
      httpMethod: GET
    mode: R
    requestCount: 1

Purpose: Reads a CSV file and uses the first item ([0,0]) in the dataset as part of the URL.

2. Read CSV with Counter

name: LoopCSVWithCounter
variables:
- name: csvData
  value: $read(path=../data/sample.csv)
  as: csv
rounds:
- name: CSVCounterRound
  numberOfClients: 5
  arrivalDelay: 3000
  iterations:
  - name: UseCSVLoop
    httpRequest:
      url: https://api.example.com/resource/${csvData[$counter(start=0, end=9),0]}
      httpMethod: GET
    mode: R
    requestCount: 10

Purpose: Uses a counter to read a new item from the CSV file for each request, iterating over the first column of each row in the first 10 rows.

3. Read a JSON File

name: ReadJSONExample
variables:
- name: jsonData
  value: $read(path=../data/sample.json)
  as: json
rounds:
- name: ReadJSONRound
  numberOfClients: 5
  arrivalDelay: 3000
  iterations:
  - name: UseJSONItem
    httpRequest:
      url: https://api.example.com/resource/${jsonData.property}
      httpMethod: GET
    mode: R
    requestCount: 1

Purpose: Reads a JSON file and uses a property (property) in the request.

4. Read an XML File

name: ReadXMLExample
variables:
- name: xmlData
  value: $read(path=../data/sample.xml)
  as: xml
rounds:
- name: ReadXMLRound
  numberOfClients: 5
  arrivalDelay: 3000
  iterations:
  - name: UseXMLItem
    httpRequest:
      url: https://api.example.com/resource/${xmlData/root/items/item[1]/subitem}
      httpMethod: GET
    mode: R
    requestCount: 1

Purpose: Reads an XML file and accesses nested elements (item/subitem) for the request.

5. Read Token from Environment Variable

name: ReadTokenFromEnv
variables:
- name: authToken
  value: $read(source=env, name=API_TOKEN)
rounds:
- name: EnvTokenRound
  numberOfClients: 1
  arrivalDelay: 1000
  iterations:
  - name: UseEnvToken
    httpRequest:
      url: https://api.example.com/secure
      httpMethod: GET
      headers:
        Authorization: Bearer ${authToken}
    mode: R
    requestCount: 1

Purpose: Reads a token from the environment variable API_TOKEN and uses it as a Bearer token in the request header.

6. Copy Token from Another Variable

name: ReadTokenFromVariable
variables:
- name: baseToken
  value: my-static-token
- name: authToken
  value: $read(source=variable, name=baseToken)
rounds:
- name: VariableTokenRound
  numberOfClients: 1
  arrivalDelay: 1000
  iterations:
  - name: UseVariableToken
    httpRequest:
      url: https://api.example.com/secure
      httpMethod: GET
      headers:
        Authorization: Bearer ${authToken}
    mode: R
    requestCount: 1

Purpose: Reads a token from another variable (baseToken) and reuses it under a new variable (authToken).