<get-var>

Variable retrieval processor

Core v2.2.0

Overview

The processor retrieves the value of a previously defined variable and returns it for use in the current execution flow. It's commonly used to pass data between different parts of a scraper configuration while preserving the original Variable type (binary data, XML structure, lists).

Usage Examples

Example 1: Simple text retrieval (both syntaxes work)

example-1.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="apiUrl" value="https://api.example.com"/>

<!-- Template syntax (simpler) -->
<http url="${apiUrl}/users"/>

<!-- Processor syntax (explicit) -->
<http>
  <get-var var="apiUrl"/>/users
</http>
</config>

Example 2: Preserving binary data (use get-var!)

example-2.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<!-- Download image as binary -->
<def var="imageData">
  <http url="https://example.com/logo.png"/>
</def>

<!-- CORRECT: get-var preserves byte[] array -->
<file path="logo.png" action="write" type="binary">
  <get-var var="imageData"/>
</file>

<!-- WRONG: ${imageData} converts to String, corrupts binary! -->
<file path="broken.png" action="write" type="binary">
  ${imageData}  <!-- ❌ Corrupted! -->
</file>
</config>

Example 3: Preserving XML structure

example-3.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="xmlData">
  <json-to-xml>
    <http url="https://api.example.com/data.json"/>
  </json-to-xml>
</def>

<!-- get-var preserves XML structure for XPath -->
<def var="username">
  <xpath expression="//user/name/text()">
    <get-var var="xmlData"/>
  </xpath>
</def>
</config>

Example 4: Working with lists

example-4.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="productIds">
  <xpath expression="//product/@id">
    <html-to-xml>
      <http url="https://example.com/products"/>
    </html-to-xml>
  </xpath>
</def>

<!-- Loop needs Variable object, not String -->
<loop item="id">
  <get-var var="productIds"/>
  
  <http url="https://example.com/product/${id}"/>
</loop>
</config>

Example 5: Dynamic variable names with template interpolation

example-5.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<def var="lang" value="en"/>
<def var="greeting_en" value="Hello"/>
<def var="greeting_pl" value="Cześć"/>
<def var="greeting_de" value="Hallo"/>

<!-- Dynamic variable name -->
<get-var var="greeting_${lang}"/>
<!-- Result: Hello (retrieves greeting_en) -->
</config>

Example 6: Reusing expensive HTTP calls

example-6.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://org.webharvest/schema/2.1/core">
<!-- Fetch API data once -->
<def var="apiData">
  <http url="https://api.example.com/data?key=${apiKey}"/>
</def>

<!-- Reuse the same response multiple times -->
<def var="userId">
  <xpath expression="//user/@id">
    <json-to-xml><get-var var="apiData"/></json-to-xml>
  </xpath>
</def>

<def var="userName">
  <xpath expression="//user/name">
    <json-to-xml><get-var var="apiData"/></json-to-xml>
  </xpath>
</def>

<def var="userEmail">
  <xpath expression="//user/email">
    <json-to-xml><get-var var="apiData"/></json-to-xml>
  </xpath>
</def>
</config>

Important Notes

Related Processors