Hoverfly Cloud
Visit Our Main SitePricingTry Hoverfly Cloud
  • Introduction
  • Use cases for API Simulation
  • Key concepts
  • What's New
  • Dashboard
  • Simulation
    • Create simulations
    • Configuring Request Matchers
      • JSON Request Matchers on the Body
        • EXACT Matcher
        • JSON Matcher
        • JSON Partial Matcher
        • JSONPath Matcher
        • Regular Expression Matcher
      • Handling the response when Hoverfly cannot match
    • Building a JSON Response
      • Using a JSON object from the Request
      • Looping over a JSON array from the Request
    • Simulating Webhooks and Callbacks
    • Using templating in simulations
      • Render back any part of the Request
      • Render synthetic data
      • Faker functions
      • Render dates and times with offsets
      • Conditional logic
      • Looping over arrays
      • Variables and Arrays
      • Arithmetic operations
      • String operations
      • Validation operations
      • Simulating a persistent backend
      • Transitioning state between requests
      • Combining and nesting templating functions
      • Useful helper functions
      • Avoiding HTML encoding in function return values
    • Using data sources in simulations
      • Querying data sources
      • Updating data in data sources
      • Deleting data from data sources
      • Inserting data into a data source
      • Guidance on using the SQL like syntax
    • Working with Simulation Files
  • Service
    • Start a new service
    • Use a service
    • Update a service
    • Configuring Journal Indexing
  • Command line interface
    • Hoverfly Cloud CLI commands
  • Tutorials
    • Quickstart
    • Creating simulations and services
      • Tutorial 1: Create a service and simulation manually
      • Tutorial 2: Create a service and simulation by importing from a Swagger specification
      • Tutorial 3: Create a service and simulation by capturing real API traffic
    • Response Templating
      • Tutorial 4: Response Templating
    • Hoverfly service modes
      • Tutorial 5: Simulate, Capture, Spy and Passthrough modes
    • Automating with the CLI and API
      • Tutorial 6: Using the CLI and the Hoverfly Cloud API
Powered by GitBook
On this page
  • Inserting new data into a CSV Data Source
  • Inserting data with {{csvAddRow}}
  1. Simulation
  2. Using data sources in simulations

Inserting data into a data source

Insert new CSV data rows on-the-fly within simulations with Hoverfly Cloud Enterprise - data is added temporarily, reverting on restart.

PreviousDeleting data from data sourcesNextGuidance on using the SQL like syntax

Last updated 3 months ago

Inserting new data into a CSV Data Source

You can insert new data rows into a CSV data source on the fly within your simulation just as if your API were inserting data into real data source. The data is added for the duration that the service is running. When the service is restarted, the data will revert to what was in the original uploaded CSV.

For illustration we will use this data source called "pets":

id
category
name
status

1000

birds

Archie

available

1001

dogs

Zipper

available

1002

dogs

Teddy

sold

Inserting data with {{csvAddRow}}

This method takes an array of strings as a parameter and adds them to the data source. You add to an array using the function {{addToArray}}. In the pets data source example there are 4 columns, and so you need to call it four times to create an array containing 4 strings.

The syntax for adding to, and reading from an array can be read

csvAddRow Syntax:

{{csvAddRow '(data-source-name)' (array-of-values)}}

Template Example: Add a new dog to the data source

{{ addToArray 'newPet' '1003' false }} 
{{ addToArray 'newPet' 'dogs' false }} 
{{ addToArray 'newPet' 'Violet' false }} 
{{ addToArray 'newPet' 'available' false }}

{{ csvAddRow 'pets' (getArray 'newPet') }}

Result:

The new pet will be added to the pets data source in memory for all subsequent calls to the API simulation. It will be modified as follows:

id
category
name
status

1000

birds

Archie

available

1001

dogs

Zipper

available

1002

dogs

Teddy

sold

1003

dogs

Violet

available

here