Skip to content

Variable Mime Type Example

Variable Mime Type Example on Github

All variables have an associated mime type to distinguish the content type of its value. This example will show two examples, and the special behaviour that happens when mimeType is text/plain or application/octet-stream.

Storing a string as a raw plaintext variable.

By default (mimeType=application/json) all variables are treated as JSON values. So this means even if you store a string in a variable, it's value is stored with quotes wrapped around it.

JSON String Data
direktiv_api: workflow/v1

description: |
  Store the workflow variable 'StringVar' as a json encoded string.  

states:
#
# Set StringVar Value: 
# "hello\nworld"
#
- id: set-var
  type: setter
  variables:
    - key: StringVar
      scope: workflow 
      value: |
        hello
        world
JSON String Variable
"hello\nworld"

If the data is YAML it will be converted to JSON in the variable.

JSON Data
direktiv_api: workflow/v1

description: |
  Store the workflow variable 'StringVar' as a json.  

states:
#
# Set StringVar Value: 
# "hello\nworld"
#
- id: set-var
  type: setter
  variables:
    - key: StringVar
      scope: workflow 
      value: 
        - key: value
JSON Variable
[{"key":"value"}]

There are certain scenarios where you would not want to store the variable with its quotes. To do this all need to do is simply set the mimeType to text/plain or text/plain; charset=utf-8. This will store the variable as a raw string without quotes.

Plain Text
direktiv_api: workflow/v1

description: |
  Store the workflow variable 'StringVar' as a plaintext string.  

states:
#
# Set StringVar Value: 
# hello
# world
#
- id: set-var
  type: setter
  variables:
    - key: StringVar
      scope: workflow 
      mimeType: 'text/plain'
      value: |
        hello
        world

Variable - StringVar Value

Plain Text Variable
hello
world

Auto-Decoding Base64 string

Another special behaviour is that it's also possible to auto decode a base64 string by setting the mimeType to application/octet-stream. This is used for binaries like Excel files, images etc.

Base64 Variable
direktiv_api: workflow/v1

description: |
  Auto decode base64 string and store the resulting value 
  as the workflow variable 'MessageVar'.  

states:
#
# Set MessageVar Value: 
# hello from direktiv
#
- id: set-var
  type: setter
  variables:
    - key: MessageVar
      scope: workflow 
      value: 'aGVsbG8gZnJvbSBkaXJla3Rpdg=='
      mimeType: 'application/octet-stream'

Variable - MessageVar Value

Binary Data
hello from direktiv

These are the only two mime types with special behaviour. Any other mimeType will be treated internally by the default JSON behaviour. The default value for mimeType is application/json