04-20-2023 4:43 AM
Hi,
In SAP Build Apps, how do I pass a CSRF token in the HTTP header of a CREATE record flow function?
To GET the CSRF token, I added a HTTP request flow function and stored the CSRF token returned by the response in an app variable.
How do I pass the app variable to the next CREATE record flow function?
When I added my data resource, I see the option called 'Common request header', where I added the header parameter name. But, I don't see an option to bind the variable.
04-20-2023 5:20 PM
I haven't tried it, but I think this will probably work:
Add the CSRFToken as an HTTP header in the REST Api configuration. Set the Label and Key to "CSRFToken", set Value type to "Text", set Is static to off or false, and Is optional to on or true. That should then add the CSRFToken to the inputs on the Create record flow function. Making the header "Not static" makes it available for a formula or variable binding in the flow function configuration.
Here's what that looks like for a get record collection call that I modified:Data Configuration
Flow Function
04-21-2023 12:00 AM
Hello @JoeBinkley ,
Thank you for the response! I am using the Odata integration to call my API.
I set my base URL here and we do not have an option to add HTTP header here unlike REST API integration. However, I see 'Common request header' option. If click this and try to make a list of values, I don't see an option to bind my CSRFToken variable. Not sure if this is the right way!
Does the approach differ for OData integration?
04-21-2023 12:01 AM
Adding another screenshot.
04-21-2023 1:31 AM
I don't have an easy way to test this, but I think this will work. You can use a formula that references an additional input.
First, add the additional input:
Then add the list item to the common request headers. (You can do the value with the formula editor)
In the logic flow, you should see this sort of property sheet:
Note, by the way, the warning below about CORS that is shown when you test the data source. I was testing whether the addition of the token would break the OData call using Web Preview and got an error message. But it did work using the Preview App on iOS.
04-21-2023 10:43 AM
@JoeBinkley : I have followed this and passed the CSRF token to the CREATE RECORD flow function. However, I received a 403 error 😞 . I don't see any OData failure logs in my S4 system as well. And yes, I have been able to test these through mobile preview only.
We had followed the same approach to test the APIs(Get token first and pass to POST) from the API enterprise hub and it worked well. Hence, not sure what is being missed when done from Build Apps.
My API is of type ODATAV4 - "Warehouse task" creation. Could you please let me know what is the advised approach to perform a POST operation from SAP Build apps for such ODATAV4 APIs? Is there any blog/post on similar ones? Most of the ones that I referred to were REST API or GET operation in OData.
Appreciate your support!
04-21-2023 10:53 AM
Are you using this API:
https://api.sap.com/api/OP_WAREHOUSEORDER_0001/path/post_WarehouseTask
Are you able to setup a BTP destination? That's generally an easier way to communicate with SAP systems.
04-21-2023 11:13 AM
Yes, that is the API I am using.
No, haven't set up the BTP destination. If we set up a BTP destination, in the build apps, do we use 'SAP BTP destination REST API integration' instead of OData integration to perform the POST operation with the API?
04-21-2023 11:58 AM
Using BTP destination is done using the ADD INTEGRATION button just above where you see "SAP Build Apps classic data entities". Using that will require your web app to use BTP Authentication. There is an issue now where BTP authentication does not work in mobile preview.
04-25-2023 4:02 AM
@JoeBinkley : Thanks for the inputs! I have been able to get this working.
The approach was same, use HTTP flow function to get the CSRF token. Additionally, pass the X-CSRF-TOKEN ~ FETCH in the header. Store the CSRF token in a variable. Then, use the 'Create record' flow function and pass the CSRF token here. To pass the CSRF token to a OData data resource, need to add an additional new input (approach is the same as in your reply).
The only difference for this API was, I created a data record and passed it.
Earlier, I was passing the field level inputs at the record level and there was a failure. Not sure if it is needed this way for this specific API. But, this is my logic in the canvas now:
Btw, the BTP destination feature works really well too and it did not involve the hassle and fetching and passing CSRF token. But, the mobile preview doesn't work and we have features like scanning a barcode on our app and those do not work in web preview. So, we have planned to stick with this workaround for now.
Thanks for the support 🙂 !
04-25-2023 5:20 AM
That's great news. I'm glad it worked out.
And, thanks for letting other Builders know how you did it!