X-CSRF-TOKEN in SAP API POST request

Hello!
We have an API that is supplied by SAP S/4HANA, where we send a POST request to insert data into the ERP. Both me and the developer I have spoken to have tried using Postman & Visual Studio and have successfully sent post requests to this API.

When trying with EasyMorph, I get an error saying that the CSRF token validation failed.

Using that same token in Visual Studio and Postman returns a 201, Successfully created.

I tried to replicate the headers exactly as I sent it with Postman in EasyMorph and what I found is that Cookies isn't supported as a header. Could this be a reason for the error?

This is how I have set it up in EM:

Appreciate any help I can get with this.

Hi.

(Iterate)WebRequest uses a special CookieContainer to receive, store, and send cookies. In the case of (Iterate)WebRequest you cannot manage cookies using the Cookie header.

The CookieContainer lifecycle is tied to the runsession. The CookieContainer is created with the runsession and deleted when the runsession is completed. It can't be preserved.

That means that the whole cycle of authentication, data processing, and data sending must be executed in a single runsession.

Try to use "Reload and run" to run the entire workflow at once.

image

1 Like

Also, in some cases, you additionally may need to tick the Don't send the header "Expect:100-continue" checkbox.

Hello ckononenko,
Thank you for replying!

My EasyMorph project only has one table and one action at the moment, since I gather the token from Postman right now (which is alive for approx. 1 hour).

I tried to tick the "Don't send the header "Expect:100-continue"", but it had no effect in this situation.
I also tried to reload and run the project, but since I only have one action in the whole project (send a post request, text format with a json payload, with the headers mentioned above) I didn't expect it to work, which it also didn't.

I also tried to request a token and pass it to my post request in EM while also using the "reload and run" function, which also didn't work.

Do you know of something else I might try?
I found a post where dgudkov were showcasing how to send a CSRF token on a login page:

But this didn't seem to work in my case.

How do you fill the x-csrf-token header here?
What is the method of obtaining this value?

As of right now, it's a copy-paste from a GET request in Postman. What my plan is once I get it to send a successful POST request in EasyMorph is to fetch the x-csrf-token from a GET request in EM and then pass it to a new module and assign it from a parameter.

I realized my error now that you mentioned it.
I tried to make the GET request and iterate it to a new module and pass the token as a parameter to my POST request and it got the validation right. The information you gave me in post #2 clicked when you asked that last question.

Thank you for your help!

1 Like