Thursday, 13 February 2020

POSTMAN- CSRF token validation failed error - Fix

If you encounter an issue in the POSTMAN when calling the POST method even though you are passed in the valid X-CSRF token by using the GET call in the previous step. Then here is the solution for your problem.

While sending the POST method, use the extra header parameters to solve this.

"CSRF token" and  "cookie" header should be picked from the GET call.

X-CSRF-Token:pIgKS5dokT0FZTouD8-jig==
Content-Type:application/json
Connection:keep-alive
Cookie:SAP_SESSIONID_KUH_246=fgAZqA33-I3QLvTa5yuXPb-AoFlObxHqhCcAFj5r60I%3d


Hope this resolves your issue. If yes, comment below. Thanks



Wednesday, 5 February 2020

Find the field from atom-xml / pick the field from unformatted xml in SAP CPI

Find the field from atom-xml / pick the field from unformatted xml in SAP CPI.

Data:

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://XXX.ondemand.com/sap/c4c/odata/cust/v1/servicelocation/" xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
  <id>https://XXX.ondemand.com/sap/c4c/odata/cust/v1/servicelocation/AddressSnapshotPostalAddressCollection</id>
  <title type="text">AddressSnapshotPostalAddressCollection</title>
  <updated>2020-02-04T10:00:13Z</updated>
  <author>
    <name/>
  </author>
  <link href="AddressSnapshotPostalAddressCollection" rel="self" title="AddressSnapshotPostalAddressCollection"/>
  <entry m:etag="W/&quot;datetimeoffset'2020-01-23T12%3A51%3A32.3360950Z'&quot;">
    <id>https://XXX.ondemand.com/sap/c4c/odata/cust/v1/servicelocation/AddressSnapshotPostalAddressCollection('00163E6CDD2F1EEA8FBBE27F8D76E336')</id>
    <title type="text">AddressSnapshotPostalAddressCollection('00163E6CDD2F1E7A8FBBE22F8D76E336')</title>
   <updated>2020-02-04T10:00:13Z</updated>
    <category term="cust.AddressSnapshotPostalAddress" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
    <link href="AddressSnapshotPostalAddressCollection('00163E6CDD2F1E7A8FBBE22F8D76E336')" rel="edit" title="AddressSnapshotPostalAddress"/>
    <content type="application/xml">
      <m:properties>
        <d:ObjectID>00163E6CDD2F1E7A8FBBE22F8D76E336</d:ObjectID>
        <d:ETag>2020-01-23T12:51:32.3360950Z</d:ETag>
        <d:CountryCode>BE</d:CountryCode>
        <d:CityName>Scherpenheuvel-Zichem</d:CityName>
        <d:StreetName>Bergstraat 1</d:StreetName>
        <d:StreetPostalCode>3272</d:StreetPostalCode>
        <d:CountyName/>
      </m:properties>
    </content>
  </entry>
</feed>

Groovy Script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
def body = message.getBody();
def data = new XmlSlurper().parseText(body);
String[] productId= " ";

def id = data.'**'.findAll { node -> node.name() == 'ObjectID' }*.text();
def country = data.'**'.findAll { node -> node.name() == 'CountryCode' }*.text();
def city = data.'**'.findAll { node -> node.name() == 'CityName' }*.text();

assert id.size() == 1;
assert country.size() ==1;
assert city.size() ==1;
message.setProperty("objectid",id);
message.setProperty("CountryCode",country);
message.setProperty("CityName",city);

return message;

}

Thanks Shruthi for helping on this solution.



Date format groovy script from UTC timezone in SAP CPI

To convert the time into the below format, we used this method for temporary fix.

Output required as : 2020-02-05 14:10:00 UTC

Groovy script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.util.TimeZone

def Message processData(Message message) {

//Body
def body = message.getBody();

//Properties
map = message.getProperties();

//LastSuccessfulRunTeleDev - variable value maintained in cpi system.
value = map.get("LastSuccessfulRunTeleDev");

def curDateTime1 = "";
def tz = TimeZone.getTimeZone("Europe/Berlin")
def ts = new Date()

curDateTime1 = (ts.format("yyyy-MM-dd HH:mm:ss", timezone=tz))

def datequery = "(CHANGE_DATE_TIME ge"+"'"+value+" UTC"+"'"+" and CHANGE_DATE_TIME le"+"'"+curDateTime1+" UTC"+"'"+")";

message.setHeader("datequery",datequery);
message.setProperty("CurrentDate",curDateTime1);
return message;

}





Monday, 11 November 2019

ODATA - unknown internal server error occured in SAP CPI


When access the ODATA service to the target system, it might give the error as below

Error in CPI response from receiver system:

<?xml version="1.0" encoding="utf-8"?><error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><code>00145E5B1CC71EE0AB8B273DA099C30F</code><message xml:lang="en">In the context of Data Services an unknown internal server error occured</message></error>


Solution:

Try accessing the same link from POSTMAN tool directly to receiver system, if it gives expected response, then in CPI, delete the receiver channel and do the configuration again to resolve this issue



Monday, 21 October 2019

Date timeZone format conversion using Groovy script

There is a requirement to convert the current date to the Europe time Zone. Standard time format is UTC.

Script:

import java.util.TimeZone;

def curDateTime1 = "";
def tz = TimeZone.getTimeZone("Europe/Berlin")
def ts = new Date() 
curDateTime1 = (ts.format("yyyy-MM-dd'T'HH:mm:ss", timezone=tz))

println curDateTime1

Output:

2019-10-21T16:25:04







Tuesday, 17 September 2019

Allowed Header in Runtime Configurations in iflow SAP CPI


Allowed Header in the integration flow in Runtime Configurations


SapAuthenticatedUserName - To identify the Username that calls the integration flow

Note: If the sender channel is configured to use client certificate authentication, no such header is set (as it is not available in this case). Only for User Role it can be used.

CamelHttpUrl - Refers to the complete URL called, without query parameters

 For Example: CamelHttpUrl:https://XXXX-iflmap.hcisbp.XXX.hana.ondemand.com/http/dev/SF/c4c/fileupload

CamelHttpQuery - Refers to the query string that is contained in the request URL.

CamelHttpMethod - Refers to the incoming method names used to make the request. These methods are GET, POST, PUT, DELETE, and so on.

 For Example: CamelHttpMethod:POST

CamelServletContextPath - Refers to the path specified in the address field of the channel.

 For Example: CamelServletContextPath:/dev/SF/c4c/fileupload

Script used to read the header values:

def headers = message.getHeaders()
def userName = headers.get("SapAuthenticatedUserName")

Result of the above script:

SapAuthenticatedUserName:S0020XXXXXXX  (in header)

------------------------------------------------------------------------------------

Cheers!!!



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.