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

adding CSRF parameter in service.tx(request).run(request.query)

karthiheyan_murugesan1
Product and Topic Expert
Product and Topic Expert
5,655

Hi ,

Any lead on how to use service.tx(request).run(request.query) for a post call to S/4 odata.

More details

When I use service.tx(request).run(request.query) for a post call, Iam getting CSRF token invalid error in S/4 and thus 403 error in CF.

Iam able to get CSRF token with S/4 URL directly in Tcode /iwfnd/maint_service with header as [X-CSRF-TOKEN : Fetch ].

Iam not able to get CSRF token if i use post using service.tx(request).run(request.query) of SAP CAP Service. [HTTP header is not having the [X-CSRF-TOKEN : Fetch] ]

Exact question

  1. How to set header parameter for getting CSRF token in [ service.tx(request).run(request.query) ] . I want to set [X-CSRF-TOKEN : Fetch] in a GET request header so i can set back CSRF token in post call.

Regards,

Karthi

View Entire Topic
jhodel18
Active Contributor
0 Likes

I’m afraid that you are going to code the logic for that on your own. According to the documentation below:

https://cap.cloud.sap/docs/guides/consuming-services#sending-requests

the fluent API can cater to do create (POST) request, however, from my experience, it didn’t work when it starts to use the service via connectivity proxy. So what I did, is I have coded the solution my self using the axios node module — it is the same node module that cap for node.js use.

karthiheyan_murugesan1
Product and Topic Expert
Product and Topic Expert
karthiheyan_murugesan1
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi ,

I tried implementing a simple sap-cf-axios get method , it was working perfectly. When i call the service second time , it is failing with 407 error. What am I missing ? Any hint ?. This looks strange. It is failing alternatively .

Regards,

Karthi

jhodel18
Active Contributor

There's an issue with the caching of token inside this module sap-cf-destconn. So what I did is I hacked the code to disable the passing back of the cached token and it is working fine after that (for subsequent calls). See screenshot below:

karthiheyan_murugesan1
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi Jhodel,

Thanks a lot 😉 🙂 :-). I see you raised an issue for this. 🙂 .

This could be dumb question, I see you corrected the .js file of sap-cf-destconn. Assume I do not want to wait for an official fix and want to move on with the local version. How should I handle it in CAP world? Because MTA will not consider my local library and package.json will always install from the library. Is there any way I can copy the library and make as it a local one ,do some config in mta.yaml to consider my local library in deployment and refer the same in service.js .

jhodel18
Active Contributor

Hi Karthiheyan,

You're welcome! About the SAP Cloud SDK, I played around it once and encountered some issues. I'm still new on this tool and haven't really taken a deep look at this. Perhaps when I have some free time, I will look into this again.

Right now I'm comfortable with my current workaround solution, but I need to work on this a little bit more to make it more elegant, what I mean by that is that the workaround solution is working when deployed to SCP, but it is not easy to test locally. Once I have made this solution more compatible with CAP, I plan to write a blog about this.

In order for you to make a local copy of the NPM delivered modules, you can create a lib folder inside your cap "srv" folder. In my case, I'm not using the module sap-cf-axios. I'm using axios + sap-cf-destconn. I copied connectivity.js in my srv > lib folder. And called it in one of my cap custom .js handlers using this syntax:

const { readConnectivity } = require("./lib/connectivity");

Note that when you specify to load a module in this way, you are loading from the local module. But if you are omitting the ./ or ../ then you are loading from a node_module folder -- like the example below:

const axios = require("axios").default;

Just bear in mind, that if the JS file you copied does call another file within that module you have to copy that JS file as well. In my case, connectivity.js is calling for tokenCache.js and that's why I copied it as well. I disabled the returning of the cache but I didn't remove all the logic that is linked to tokenCache.js. I also cannot wait for a permanent fix for the module so I opted to handle it myself in my own cap project and it is already deployed in SCP.

karthiheyan_murugesan1
Product and Topic Expert
Product and Topic Expert

Thanks a lot Jhodel.Sincere thanks for sharing your knowledge.

jhodel18
Active Contributor

I was pumped up to finish fixing the code. By this point, it will be easy for you to consume my solution because I already published it in npmjs.com -- see below git repo, and refer to the readme documentation how to install and use it.

https://github.com/jcailan/cdse

Happy to help Karthiheyan!

karthiheyan_murugesan1
Product and Topic Expert
Product and Topic Expert

Thanks a lot Jhodel. I want one more greedy request :-). I think this is not considering the Location id parameter in Destination . When Destination has Location id , this is not able to identify the SCC and resulting in 503 error. I tried configuring a new connection without Location id and it is working for all subsequent calls.

Make this wish true when you have time.Thanks a lot Jhodel.

jhodel18
Active Contributor
0 Likes

Hi Karthiheyan, no problem! Yeah, I did not include LocationId in the logic because I'm not using it, and therefore, not able to test. I've applied patch 1.0.2 -- can you try it out and let me know if it works?

karthiheyan_murugesan1
Product and Topic Expert
Product and Topic Expert
0 Likes

Thanks a lot, Jhodel. The latest update is working fine.

Regards,

Karthi

SumitKundu
Active Participant
0 Likes

jhodel18 Thanks for this wonderful extension node module! Is this still an issue with the CAP's own node.js library?

Best regards.

Sumit