cancel
Showing results for 
Search instead for 
Did you mean: 

How to add header to payload using groovy in sap cpi

mounikaravilla
Explorer
0 Kudos

Hi All,

I was wondering on how to add a header to payload as an extra node using groovy.

Have tried append node. Since we can get headers in hashmap.(but it didnt work)

Want to know how to append a header or property as a part of main XML.

Please suggest

Accepted Solutions (1)

Accepted Solutions (1)

Sriprasadsbhat
Active Contributor
0 Kudos

Hello Mounika,

Below should work.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.XmlUtil.*;

def Message processData(Message message) {
    def body = message.getBody(java.lang.String);
    def map = message.getHeaders();
    def value = map.get("Threshold");	 
    def Threshold = "<Threshold>"+value+"</Threshold>"
    
    def response= new XmlSlurper().parseText(body)
    def newNode = new XmlSlurper().parseText( Threshold )   

    response.MessageProcessingLog.appendNode( newNode );
    def outxml = groovy.xml.XmlUtil.serialize( response )
    message.setBody(outxml)   
    return message;
}

Regards,

Sriprasad Shivaram Bhat

Answers (2)

Answers (2)

former_member417122
Discoverer
0 Kudos

Hi Mounika,

You need to initialize hash map object and get the value . Please try with to initialize and then check

Sriprasadsbhat
Active Contributor
0 Kudos

Hello Mounika,

Adding below to your question might help you to get some answers.

1) Input XML or Data.

2) What you have tried so far ( may be code snippet you have written or any method you have tried ).

3) Output Expected .

Regards,

Sriprasad Shivaram Bhat

mounikaravilla
Explorer
0 Kudos

Hi Sriprasad,

Thank you
Below are the details
Data:
  1. <MessageProcessingLogs> <MessageProcessingLog> <Status>FAILED</Status> <LogStart>2020-07-28T04:07:56.806</LogStart> <MessageGuid>XXXXXX</MessageGuid> <LogEnd>2020-07-28T04:07:56.850</LogEnd> <IntegrationFlowName>XXXXXXX</IntegrationFlowName> </MessageProcessingLog> </MessageProcessingLogs>

The Header is

Threshold =5

Groovy used:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import org.custommonkey.xmlunit.*;
import org.w3c.dom.NodeList;
import javax.xml.xpath.*;
import javax.xml.transform.TransformerFactory;
import org.w3c.dom.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.util.LinkedHashMap;
def Message processData(Message message) {
    def body = message.getBody();
    def response= new XmlSlurper().parseText(body)
    x= response.MessageProcessingLogs
    def map = message.getHeaders();
     def value = map.get("Threshold");
  def res = x.appendNode(value); 
    message.setBody(res)   
      return message;
}

Output Expected:

<MessageProcessingLogs> <MessageProcessingLog> <Status>FAILED</Status> <LogStart>2020-07-28T04:07:56.806</LogStart> <MessageGuid>XXXXXX</MessageGuid> <LogEnd>2020-07-28T04:07:56.850</LogEnd> <IntegrationFlowName>XXXXXXX</IntegrationFlowName> </MessageProcessingLog><Threshold>5</Threshold> </MessageProcessingLogs>

That was the requirement to add the header "Threshold" to the nodes of XML