← Back to Groovy & JavaScript

Working with Message Headers and Properties

In SAP CPI, Message Headers and Message Properties are crucial for storing and sharing data throughout an integration flow. While they both store key-value pairs, they have different use cases and lifecycle.

Headers vs. Properties: What's the Difference?

  • Headers: Are a fundamental part of the message itself, often containing technical information like HTTP request headers. They are typically used for temporary, transport-related data and are passed with the message payload from one step to another.
  • Properties: Are part of the integration flow's execution context. They persist for the entire duration of the message processing and can be accessed by any step in the flow. They are ideal for storing business-specific data or intermediate values that you need to reference later.

Accessing and Manipulating Data in Groovy

You can use the Message object to access both headers and properties. The methods are very straightforward: 'getHeader()', 'getProperties()', 'setHeader()', and 'setProperty()'.

Example Groovy Script:

import com.sap.gateway.ip.core.customdev.util.Message

def Message processData(Message message) {

    // --- Working with Headers ---
    // Get a header value
    def contentType = message.getHeader('Content-Type')
    
    // Set a new header
    message.setHeader('sap-cpi-user', 'ashutosh-v')

    // Remove a header
    message.removeHeader('Content-Length')

    // --- Working with Properties ---
    // Get a property value
    def vendorId = message.getProperty('Vendor_ID')
    
    // Set a new property
    message.setProperty('ProcessedBy', 'GroovyScript')
    message.setProperty('Timestamp', new Date())
    
    // Get all properties as a map
    def allProperties = message.getProperties()

    return message
}

By effectively using headers and properties, you can create modular and robust integration flows, avoiding complex logic in a single script and instead passing data seamlessly between different steps.