Monday 2 September 2019

Logging Cookie for session handling Csrf Token - SAP CPI

There is a required step which need to used, when using the CSRF-Token mechanism. To fetch the CSRF- token, the external system will be called and retrieves the token. Session need to be enabled at header to maintain the token to push the actual data to external system.

To Log the Cookie, use the below code after the Get CSRF token request step.

Groovy Script:


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

def Message processData(Message message)
{
def headers = message.getHeaders();

// read cookies to send with next request, especially yMkt Session ID
def cookie = headers.get("Set-Cookie");

StringBuffer bufferedCookie = new StringBuffer();

// bring cookies in right format

for (item in cookie)
{
bufferedCookie.append("\"" + item + "\"" + "; ")
}

message.setHeader("Cookie", bufferedCookie.toString());

def messageLog = messageLogFactory.getMessageLog(message);

if(messageLog != null)
{
messageLog.setStringProperty("Logging_Cookie", bufferedCookie.toString());
}

return message;
}

Hope this helps.



No comments:

Post a Comment