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
  • Configuring Journal Indexing on a Service (for Enterprise accounts)
  • Important Considerations
  1. Service

Configuring Journal Indexing

This provides the useful illusion of a persistent back-end.

PreviousUpdate a serviceNextCommand line interface

Last updated 1 year ago

Configuring Journal Indexing on a Service (for Enterprise accounts)

This feature gives the user the ability to retrieve any part of a previously received request, or served response on the same service, and render it.

This provides the useful illusion of a persistent back-end. It uses it's in-memory journal which is a history of previous requests/responses processed, to achieve this illusion, hence this templating function's name {{ journal }}

As an example, this will give you the ability to have a POST or PUT endpoint which takes a product with a specific ID and then for example a GET endpoint on the same service which will retrieve the previously stored product based on it's ID and respond with it.

In the above example the service is told to index the journal based on the productId in the request body’s json.

When the first PUT request is made containing a productid and productDescription, Hoverfly stores the Request/Response pair in the journal and gives it an index key of 1.

Later on the client makes a GET to a different endpoint with the number 1 stored in the path at zero based index position 2. The template responds by calling the journal templating function, and telling it to use the index on the productId, and to retrieve the Request/Response pair using the index value that was passed in.

Once retrieved it renders the productDescription from the Response, using a jsonPath query.

Selecting the Edit Journal Indexing menu item from the ellipse to the right of the service on the Dashboard brings up the Configure Journal Indexing modal:

This capability is achieved through Hoverfly Cloud indexing all previous requests and responses according to a key that you provide, which identifies a part of a request. In the above example it is a query parameter called productId. Other valid examples might be:

Request.Body 'jsonpath' '$.productId' (If your request has a json element called productId in the body)

Request.Body 'xpath' '//root/productId' (If your request has an xml element called productId in the body)

A templating function is provided which then allows you to render any part of a previous request or response according to that key. The function is structured as follows:

{{ journal <request-index> <key-value-to-search-for> <request-or-response?> <query-type> <query-to-execute> }}                    

Here are a couple of examples of using the {{ journal }} function:

Example 1: Note the use of single quotes and brackets as required:

  • <request-index> 'Request.Body jsonpath $.productId' - the literal string is passed in

  • <key-value-to-search-for> (Request.Path.[1]) - this is a function therefore enclosed in ( )

  • <request-or-response> 'Response' - the literal string is passed in

  • <query-type> 'jsonpath' - the literal string is passed in

  • <query-to-execute> '$.productDescription' - the literal string is passed in

{
  "productDescription": "{{ journal 'Request.Body jsonpath $.productId' (Request.Path.[1]) 'Response' 'jsonpath' '$.productDescription' }}"
}

Example 2: Similarly, note that strings are enclosed in quotes and functions in ( )

<?xml version="1.0" encoding="UTF-8"?>
<productDescription>{{ journal 'Request.Body xpath //productId' (Body.Request 'xpath' '//productId') 'Response' 'xpath' '//productDescription' }}</productDescription>

Important Considerations

The journal templating feature uses an in-memory history of the previous 1000 requests and corresponding responses processed by the API. The oldest requests are purged from memory once more than 1000 requests are processed. Thus to work correctly, your dataset must not exceed a maximum of 1000 unique items when using journal templating.