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:
In the
resource
element for the form resource, add themediatypes
element and set it totext-csv
.In the
entity
element for the form resource, within theproperty
element, add anarray
element. Specify the type of entity to use for the array.note
You can have only one
array
element.In the
entity
element for array entity, defineproperty
elements that match the names of the data headers in the CSV file. Properties must be defined in terms of primitive types, such asinteger
orstring
.
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
.
In the
add-items-to-cart-from
resource, add themediatypes
element and set it totext-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>
In the
add-items-to-cart-form
entity, within theitems
property, add anarray
with an entity type ofitem
:<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>
In the
item
entity, addproperty
elements, where thecode
property is astring
and thequantity
property is aninteger
:<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.