cancel
Showing results for 
Search instead for 
Did you mean: 

CAP: How to call remote service with same XSUAA in background job

kosmopilot
Explorer
98

I have two services A and B in different CAP apps that share the same XSUAA. I'd like service A to call the remote service B in a background job. This results in a 401 error as there is no auth token. 

  cds.spawn({}, async () => {
      const serviceB = await cds.connect.to("B"); 
      await serviceB.send("foo", {}); // 401 error
    });

Service B does have a destination configured

  "requires": {
    "B": {
      "kind": "odata",
      "model": "./srv/external/B",
      "[production]": {
        "credentials": {
          "path": "/api/",
          "destination": "b-api",
          "forwardAuthToken": true
        }
      },
  }

Remote calls to service B work as intended when initiated inside request handling, as there is an auth token to be forwarded.

serviceA.on("bar", async () => {
  const serviceB = await cds.connect.to("B");
  await serviceB.send("foo", {}); // works
});

Is there a way for CAP to handle this automatically or do I have to fetch the token manually and set it in the header?

Accepted Solutions (0)

Answers (0)