SAP / cloud-sdk-js

Use the SAP Cloud SDK for JavaScript / TypeScript to reduce development effort when building applications on SAP Business Technology Platform that communicate with SAP solutions and services such as SAP S/4HANA Cloud, SAP SuccessFactors, and many others.
Apache License 2.0
164 stars 56 forks source link

SDk library ->@sap-cloud-sdk/http-client #4949

Closed zarar1210 closed 1 month ago

zarar1210 commented 2 months ago

We are using Cloud SDk library ->@sap-cloud-sdk/http-client - npm (npmjs.com) and it function executeHttpRequest to send request to convergent mediation system(On premise ) via cloud connector .The connectivity is made using Destination which is mainatained in BTP subaccount.

Issue : when we send request to Microservice on particular endpoint. Cloud connector logs shows three requests being fired at the same time even though we send the request once.

const { executeHttpRequest } = require('@sap-cloud-sdk/http-client');
const express = require('express'); const app = express();
app.use(express.json());

app.post('/xyz', async (req, res) => {
   try {
       async function callPostApi(req, destination) {
           const response = await executeHttpRequest(
               {
                   destinationName: destination,
               },
               {
                   method: 'POST',
                   url: req.url,
                   data: req.body,
                   maxContentLength: Infinity,
                   maxBodyLength: Infinity,
                   headers: {
                       "Content-Type": "application/json"
                   },
               }
           );
           return response.data;
       }
       const responseData = await callPostApi(req, "Destination Name");
       res.status(200).send(responseData);

   } catch (error) {
       console.error('Error:', error);
       res.status(500).send({ message: "An error occurred", error });
   } });
tomfrenken commented 2 months ago

Hi @zarar1210,

The way you are using the SAP Cloud SDK seems correct, however, the cause of your issue is improbable to be the SDK. Without knowing the rest of your setup it is hard to judge, I would advice you to attach a remote debugger to your application, we have some documentation on this here.

I would assume that you are executing executeHttpRequest three times due to some malfunction in your setup, therefore you could simply create breakpoint at await executeHttpRequest.

Please let me know if this already helped you in finding the cause.

zarar1210 commented 2 months ago

Hi @tomfrenken

This is the final code , Currently I'm using SAP BAS, How I can debug in SAP Business Application Studio?, Please share me if any document is available for this .

const { executeHttpRequest } = require('@sap-cloud-sdk/http-client');
const express = require('express'); 
const app = express();
app.use(express.json());

app.post('/xyz', async (req, res) => {
   try {
       async function callPostApi(req, destination) {
           const response = await executeHttpRequest(
               {
                   destinationName: "TEST",
               },
               {
                   method: 'POST',
                   url: req.url,
                   data: req.body,
                   maxContentLength: Infinity,
                   maxBodyLength: Infinity,
                   headers: {
                       "Content-Type": "application/json"
                   },
               }
           );
           return response.data;
       }
       const responseData = await callPostApi(req, "Destination Name");
       res.status(200).send(responseData);

   } catch (error) {
       console.error('Error:', error);
       res.status(500).send({ message: "An error occurred", error });
   } });
tomfrenken commented 2 months ago

Hi @zarar1210 your code looks overall functional (although defining the function within the try catch block seems rather weird to me).

I found this blog post for BAS, it seems like the steps involved are very similar / identical.

marikaner commented 2 months ago

@zarar1210, can you check whether these requests are CSRF token fetching requests? For POST this is done automatically, you can turn it off as described here: https://sap.github.io/cloud-sdk/docs/js/features/connectivity/http-client#csrf-token-fetching. You should also be able to see this in the logs.

zarar1210 commented 2 months ago

Hi @marikaner , Issue resolved now for POST using this { fetchCsrfToken: false }

What about GET method ? No need to add fetchCsrfToken ?

tomfrenken commented 1 month ago

No, as you can also see in the references docs:

It is not available for GET requests.