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
resourceelement for the form resource, add themediatypeselement and set it totext-csv.In the
entityelement for the form resource, within thepropertyelement, add anarrayelement. Specify the type of entity to use for the array.note
You can have only one
arrayelement.In the
entityelement for array entity, definepropertyelements that match the names of the data headers in the CSV file. Properties must be defined in terms of primitive types, such asintegerorstring.
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-fromresource, add themediatypeselement 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-formentity, within theitemsproperty, add anarraywith 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
itementity, addpropertyelements, where thecodeproperty is astringand thequantityproperty 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.