Skip to Content

How to add header to payload using groovy in sap cpi

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

Add a comment
10|10000 characters needed characters exceeded

Related questions

3 Answers

  • Best Answer
    Posted on Jul 31, 2020 at 10:14 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 30, 2020 at 05:04 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

    • 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

  • Posted on Jul 31, 2020 at 09:13 AM

    Hi Mounika,

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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.