‎2024 May 31 6:13 AM - edited ‎2024 May 31 6:35 AM
Hi, I am attempting to call an SAP OData service from a CAP (Cloud Application Programming) application using a CDS spawn job. I am utilizing a destination with Principal Propagation but encountering the following error:
logs:-
[cds] - connect to sap_odata > odata-v2 { destination: 'sap_destination', forwardAuthToken: true }
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR [remote] - Error: Error during request to remote service:
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR Failed to load destination.
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at run (/home/vcap/app/node_modules/@sap/cds/libx/_runtime/remote/utils/client.js:310:31)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at async RemoteService.<anonymous> (/home/vcap/app/node_modules/@sap/cds/libx/_runtime/remote/Service.js:276:20)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at async next (/home/vcap/app/node_modules/@sap/cds/lib/srv/srv-dispatch.js:76:17)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at async RemoteService.handle (/home/vcap/app/node_modules/@sap/cds/lib/srv/srv-dispatch.js:74:10)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at async Object.methodName(/home/vcap/app/srv/util/myfile.js:102:24)
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR at async mycatfile (/home/vcap/app/srv/util/mycatfile.js:524:22) {
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR statusCode: 502,
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR reason: {
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR message: 'Error during request to remote service: \n' +
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR "Failed to load destination. Caused by: No user token (JWT) has been provided. This is strictly necessary for 'PrincipalPropagation'.",
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR request: {
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR method: 'POST',
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR url: '/xxxx/xxxxxx_SRV/dummyEntitySet',
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR headers: {
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR accept: 'application/json,text/plain',
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR 'accept-language': 'en',
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR 'content-type': 'application/json',
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR 'content-length': 311,
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR authorization: 'Bearer ...'
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR }
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR },
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR correlationId: '507a3d4f-bf6d-4296-4cc1-f6b8f070ee2b'
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR }
2024-05-29T21:07:57.42+0530 [APP/PROC/WEB/1] ERR }
code :-
const myFn = async (req, data) {
cds.spawn({
user: cds.context?.user,
tenant: cds.context?.tenant,
headers: cds.context?.headers || req?.headers,
after: iafterTime }, async (req) => {
sendToSAP(req, customID)
})
return 'processing in backgorund'
}
const sendToSAP = async (req, customID) => {
const payload = {}
const oSAPService = await cds.connect.to("sap_destination")
let headers = { "Content-Type": "application/json" }
headers.authorization = cds.context?.headers || req?.headers
const response = await oSAPService.send({
method: "POST",
path: "/xxxx/xxxxxx_SRV/dummyEntitySet",
data: payload,
headers: headers
});
}
Request clarification before answering.
Hi @satya-dev ,
Looking at your provided snippet, the following seems to be wrong:
headers.authorization = cds.context?.headers || req?.headers
I think you meant to set it to headers.authorization.
You need to make sure, that inside your remote-service handler, the authorization header is correctly set.
Also, you should await your remote service call:
await sendToSAP(req, customID)
Best regards,
David
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For Reference - this is how to send a Request to the on-premise Server using Principal Propagation.
In my case
const { retrieveJwt } = require("@sap-cloud-sdk/core");
const cds = require("@sap/cds");
const { executeHttpRequest } = require('@sap-cloud-sdk/http-client');
const urlWithParams = "/SomeEntitySet('" + somekey + "')?myurlparams=whatever";
let reqHeaders = { "Content-Type": "application/json" }
const jwt_token = retrieveJwt(req);
const result = await executeHttpRequest({
destinationName: cds.requires.onpremisesystem.credentials.destination,
jwt: jwt_token
},{
method: 'GET',
url: cds.requires.onpremisesystem.credentials.path + urlWithParams,
headers: reqHeaders
});This also gives you the full control of the response, so that you can read the headers for example. Results will be in payload
let the_header = result.headers["some-response-header"]; let odata_answer = result.data.d;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 7 | |
| 7 | |
| 6 | |
| 5 | |
| 5 | |
| 3 | |
| 3 | |
| 3 | |
| 3 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.