cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Access external sources from Kyma function

lucasmillbrodt
Explorer
0 Likes
2,387

Dear all,

I am experimenting in the Kyma environment and I would like to access an external OData-service (in this case one that is anchored in our C4C) in a Node.js-function. I can see and use the credentials saved in the environment variables, however, I am uncertain which JavaScript-function can be used in this environment to access the external source.

So far, my function looks like this. Upon contacting the API-Endpoint directing to this function, I receive my username of the C4C (as saved in the environment variables) and the unique entity-id as served by the C4C after firing the Event Notification.

module.exports = { 
  main: function (event, context) {
    // access to environment variables
    var oCredentials = JSON.parse(process.env.CONFIGURATION);
    var sUser = oCredentials.credentials.username;
    
    // make the incoming JSON readable by Node.js and select the Unique-ID (Entity-ID)
    var sEventData = event.data.replace(/-/g, '');
    var oEventNotification = JSON.parse(sEventData);
    var sEntityId = oEventNotification.data.entityid;
    return sUser + " " + sEntityId;
  }
}

If I simulate this code by sending a payload via Postman, I see, that I get the values I selected. My next step would be fetching the corresponding data with this entity-id from the OData Service. Is this possible in this manner?

Also, as you can see, my C4C is already recognized by the Kyma-environment:

All the best

Lucas Millbrodt

View Entire Topic
jamie_cawley
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi Lucas,

When you are sending the post most sure you include the cookies received in the response for obtaining the x-csrf-token.

Regards,

Jamie

lucasmillbrodt
Explorer
0 Likes

Hey Jamie,

do you have a suggestion on how to implement the inclusion of cookies via the Node.js-Code?

Best regards

Lucas

jamie_cawley
Product and Topic Expert
Product and Topic Expert
0 Likes

The values should be in the set-cookie headers, just copy and send them the same way you are doing with the x-csrf-token.

Regards,

Jamie

lucasmillbrodt
Explorer
0 Likes

Hi Jamie,

I set the cookies acoordingly, as to be seen in this screenshot:

Sadly, the error message remains the same (It says CSRF token validation failed):

Best regards

Lucas

jamie_cawley
Product and Topic Expert
Product and Topic Expert

Try setting the headers with

headers: {
"x-csrf-token": resp.headers['x-csrf-token'],
        "Cookie": resp.headers["set-cookie"].join(";")
      },


lucasmillbrodt
Explorer
0 Likes

Thank you Jamie,

this solved my problems.

All the best

Lucas