Skip to Content

Hello SAP Community,

I would like to share this simple code snippet to send data to SAP from Node js, I have to say that I had to read a little about the node js library “request” and cookies handle concepts because I didn’t find not much information googling about cookies handling in this particular case specially about the need to pass the cookies between GET and POST method, initially I was just only passing the token but nothing about the cookies, that’s why it didn’t work at the beginning (Error HTTP 403 Forbidden), this example includes the way to pass the token and the cookies info. If you try your service with Postman rest client it’s not clear that you are going to need to handle cookies in your code because Postman do this for you.

The j variable es where we store the cookies, we get it in GET method, and pass it to the POST method with jar parameter, the same for the x-crsf-token in header parameter.

The SAP service is an odata service, the authentication is basic, the pData parameter contains the json entity to be pass as the body of the POST request.

var request = require('request');
async function doCall(pData){
  var token;
  var j = request.jar();

  var postDataToSAP = function(){
    return new Promise(function(resolve, reject){
      request({ 
                url : url, 
                jar : j,
                headers: { 
                            "Authorization": "Basic USERANDPWINBASE64",
                            "x-csrf-token" : "Fetch" 
                }
              }, function(error, response, body){
                    token = response.headers["x-csrf-token"];
                  	console.log("Obtenido el token csrf");
                    request({
                                    url:url,
                                    method: 'POST',
                                    jar: j,
                                    headers:{
                                              "Authorization":"Basic USERANDPWINBASE64",
                                              "Content-Type":"application/json",
                                              "X-CSRF-Token":token, // set CSRF Token for post or update
                                    },
                                    json: pData
                            }, function(error, response, body){
                                  console.log("Datos de indicadores transferidos a SAP de forma exitosa");            
                                  resolve();
                            });     
                  });
    });    
  }
  
  await postDataToSAP(); 
}

Now my challenge is to call the same service but published in SCP with odata provisioning, I say that it’s a challenge because I can’t use basic authentication there, SCP doesn’t allow this when we are using a custom IdP (we are using SAP Cloud Identity), only with SAP ID Service it’s posible, so I have to study saml2 protocol to see what’s the way to authenticate, I will be in touch with the community when I can do this.

Please if you have any question don’t hesitase to ask.

Best regards.

Jhon Jairo.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply