Skip to main content
Version: 8.8.x

Supporting CSV Files

You can create form resources that support comma-separated values (CSV) files. When you define a form resource to support the text/csv mediatype, Cortex can accept and return CSV files that contain data in the expected format. Add support for CSV files in the resourceʼs API Definition.

To add support, the resource API definition must include the following elements:

  1. In the resource element for the form resource, add the mediatypes element and set it to text-csv.

  2. In the entity element for the form resource, within the property element, add an array element. Specify the type of entity to use for the array.

    note

    You can have only one array element.

  3. In the entity element for array entity, define property elements that match the names of the data headers in the CSV file. Properties must be defined in terms of primitive types, such as integer or string.

The following example shows you in detail how to add the elements to the resource API definition. The structure of your XML code must adhere to the patterns used in the example.

Example add-items-to-cart-form with support for text/csv​

The following example walks you through defining an add-items-to-cart-form form resource that accepts a CSV file. The data in the CSV file is structured as code, quantity.

  1. In the add-items-to-cart-from resource, add the mediatypes element and set it to text-csv:

    <resource>
    <name>add-items-to-cart-form</name>
    <description><![CDATA[The form to add items to the cart.]]></description>
    <uri>/{carts.cart}/form</uri>
    <form>
    <entity>add-items-to-cart-form</entity>
    <action-rel>additemstocartaction</action-rel>
    <result>carts.cart</result>
    </form>
    <mediatypes>
    <text-csv/>
    </mediatypes>
    </resource>
  2. In the add-items-to-cart-form entity, within the items property, add an array with an entity type of item:

    <entity>
    <name>add-items-to-cart-form</name>
    <description><![CDATA[The form to add items to the cart.]]></description>
    <property>
    <name>items</name>
    <description><![CDATA[The items.]]></description>
    <array>
    <is-a>item</is-a>
    </array>
    </property>
    <entity-type>carts.add-items-to-cart-form</entity-type>
    </entity>
  3. In the item entity, add property elements, where the code property is a string and the quantity property is an integer:

    <entity>
    <name>item</name>
    <description>Item entity</description>
    <property>
    <name>code</name>
    <description><![CDATA[The sku code of the item to add to cart.]]></description>
    <string/>
    </property>
    <property>
    <name>quantity</name>
    <description><![CDATA[The total number of items to add to cart.]]></description>
    <integer/>
    </property>
    <entity-type>carts.item</entity-type>
    </entity>

Cortex can now accept and return CSV files for the add-items-to-cart-form resource.

For a working example, see the Helix Pattern Library.