Home > Forum > Data sources > REST API connection to SAP

REST API connection to SAP
0

Dear Community,
I'm trying to connect with REST API to SAP API.
According to SAP API documentation this should look like this:

POST https://localhost:50000/b1s/v1/Login

{
"CompanyDB": "SBODEMOUS",
"Password": "1234",
"UserName": "manager"
}

In response I receive receive 2 HTTP Cookie items (B1SESSION and ROUTEID) that I have to submit with every REST API call to SAP API.

The problem is that I can't submit JSON configuration in REST API Connections area in WEBCON BPS Designer Studio. And later use it to setup REST API Data sources.

Maybe someone came up with solution how to deal with it?

MVP

Hi Adam,

It seems that you can pass the information as headers.
https://stackoverflow.com/questions/73777371/how-to-make-subsequent-request-to-sap-business-one-service-layer-from-django

headers = {
"Cookie": "B1SESSION=SessionID; ROUTEID",
"authkey": "XXXXXXXXXXXXXXXXXXXX",
"Accept": "*/*",
"Content-Type": "text/plain",
}

I have two idea which should work and a few other which could be validated.

1. Custom data source
This one should work for sure, but requires more development. Depending on your use cases and may be worthwhile to explore it.
https://github.com/WEBCON-BPS/develop-docs/blob/main/WebCon.BpsExt.Examples/WebCon.BpsExt.QuickStart/CustomDataSourceExample/CustomDataSourceExample.cs

2. Technical workflow
This may be an option, if the returned values are valid for a longer time.
a) Create a technical workflow, which executes the login and stores the result in a technical field.
b) A cyclical action starts the technical workflow every no one then during a working day. Whatever this may be for you. :)
c) A global business rule returns the value(s)
d) The data source uses the business rule.


Other ideas
2.b
I have not tested, whether executing a data source used in a choose field would use an unsaved field value, when executing the REST request.
If this would work you could:
c) Store the values from the technical workflow in two field during page load
d) Use the "All form fields" option from the expression editor in the data source to reference these fields. This would also allow you to use the raw value stored in the field.


3) Login data source
a) Create a data source which returns the whole response in a field. I have always mapped the response to individual variables but I wonder, whether we can can just declare that the result is not an object with multiple properties but store the whole result in one value.
What I mean with this is that a result like would typically be mapped as
{
property1 : "some value" // not mapped
object1 :
{
"o1.p1" : "abc" // mapped to object1_property1
"o1.p2" : "abc" // mapped to object1_property2
}
}
And we would configure the result like
{
property1 : "some value" // not mapped
object1 : mapped to object1
}
b) Create a global business rule which uses the "Data source value" function and processes the result to return the values.
c) Use the global business rule in the data source.
I'm also wondering what would happen if you execute the "Data source value" function twice to return the B1SESSION and ROUTEID separately. Would they be valid, even if they are returned by two different "logins"? If they are, this would make this a whole lot easier and would allow us to configure the login data source as usual.


4) Custom SDK business rule SDK.
Instead of creating a customer data source, you could also opt for just creating a custom business rule, which executes the login and returns the necessary information.


Depending on the way and characters in the values, you may also run into this.
https://community.webcon.com/forum/thread/5645/15
If you would be running 2022 you would be fine.

Best regards,
Daniel

In reply to: Daniel Krüger (Cosmo Consult)

Hi Adam,

It seems that you can pass the information as headers.
https://stackoverflow.com/questions/73777371/how-to-make-subsequent-request-to-sap-business-one-service-layer-from-django

headers = {
"Cookie": "B1SESSION=SessionID; ROUTEID",
"authkey": "XXXXXXXXXXXXXXXXXXXX",
"Accept": "*/*",
"Content-Type": "text/plain",
}

I have two idea which should work and a few other which could be validated.

1. Custom data source
This one should work for sure, but requires more development. Depending on your use cases and may be worthwhile to explore it.
https://github.com/WEBCON-BPS/develop-docs/blob/main/WebCon.BpsExt.Examples/WebCon.BpsExt.QuickStart/CustomDataSourceExample/CustomDataSourceExample.cs

2. Technical workflow
This may be an option, if the returned values are valid for a longer time.
a) Create a technical workflow, which executes the login and stores the result in a technical field.
b) A cyclical action starts the technical workflow every no one then during a working day. Whatever this may be for you. :)
c) A global business rule returns the value(s)
d) The data source uses the business rule.


Other ideas
2.b
I have not tested, whether executing a data source used in a choose field would use an unsaved field value, when executing the REST request.
If this would work you could:
c) Store the values from the technical workflow in two field during page load
d) Use the "All form fields" option from the expression editor in the data source to reference these fields. This would also allow you to use the raw value stored in the field.


3) Login data source
a) Create a data source which returns the whole response in a field. I have always mapped the response to individual variables but I wonder, whether we can can just declare that the result is not an object with multiple properties but store the whole result in one value.
What I mean with this is that a result like would typically be mapped as
{
property1 : "some value" // not mapped
object1 :
{
"o1.p1" : "abc" // mapped to object1_property1
"o1.p2" : "abc" // mapped to object1_property2
}
}
And we would configure the result like
{
property1 : "some value" // not mapped
object1 : mapped to object1
}
b) Create a global business rule which uses the "Data source value" function and processes the result to return the values.
c) Use the global business rule in the data source.
I'm also wondering what would happen if you execute the "Data source value" function twice to return the B1SESSION and ROUTEID separately. Would they be valid, even if they are returned by two different "logins"? If they are, this would make this a whole lot easier and would allow us to configure the login data source as usual.


4) Custom SDK business rule SDK.
Instead of creating a customer data source, you could also opt for just creating a custom business rule, which executes the login and returns the necessary information.


Depending on the way and characters in the values, you may also run into this.
https://community.webcon.com/forum/thread/5645/15
If you would be running 2022 you would be fine.

Best regards,
Daniel

Dear Daniel,
Thank you for your response. Initially, my approach was similar to your suggestion regarding the technical workflow (point no. 2), but I hadn’t considered using a global business rule.

Unfortunately, the response from the SAP API doesn’t include the “ROUTEID” in the body, as it’s returned in the header, which I can’t access directly through the REST API configuration. However, I was able to retrieve it via SQL from the [LOG_AdditionalMessage] column in [dbo].[WFActionExecutions], where I extract it from the string.

To avoid creating excessive elements in the database, I’ve configured a timeout on the step of the workflow. This way, instead of generating multiple elements, I maintain a single element per day with several history versions (one for each timeout execution).

While this approach works most of the time, occasionally, the “B1SESSION” and “ROUTEID” seem to expire prematurely, despite the default session timeout being set to 30 minutes. I plan to reach out to the SAP support team to extend the session timeout to a longer period.

There are still a few technical issues I need to resolve, but I must admit I’m a bit disappointed that there’s no option to add custom headers in the REST API connection setup.

Thanks again for your support.

WEBCON