7
Views
3
Comments
Solved
Create a timed action to get API rest data
Application Type
Traditional Web
Platform Version
11.0.1000.1

Hi, I'm currently making a project for an OutSystems course, one of the requisites is to consume a Rest Api and the teacher told us that we should save the data of the API to local entities (to avoid problems if the API would be down).

The API that I'm consuming works perfectly whenever I make a single request, but when I make cycles to save all possible data into local entities the thing does not work properly. The log gives me an 403 error... When I try to set manually some id to save some data, sometimes it works, other not.... Sometimes it even saves some data for a few seconds, and then the data disappears, this is kind a strange behaviour.... I'm a bit lost here, can someone give me some lights?

Thanks

Rank: #56
Solution

Hi João,


I am glad I could help.

I don't know which API you are consuming to know if you have a more optimal approach than getting all the records 1-by-1. As I mentioned, usually this kind of imports are made by getting a subset of data from the API (as opposite to each record at a time) to avoid all these subsequent calls.


If you really need to do all of these calls and you see that you're getting the 403 due to a lot of calls consecutively, you may force a few seconds of wait between each API call (using for instance the Sleep forge component).


Kind Regards,
João

Rank: #56

Hi João,


If it save some data and then it disappears, it's because you're having an exception and you're data is being roll backed. This is a common scenario of exception handling in a for each loop and you can make sure that data is persisted in the database using the CommitTransaction action from System:

  


About you getting 403 on some calls, 403 HTTP status code means Forbidden (see more here), so it seems like you are accessing to some data that you don't have access to. It may also be that the API you are calling starts blocking if it receives too many calls in a short period of time to avoid outage, but that depends on the API you are consuming.


In any case, the usual scenario is that you get all the data (or paginated data) from the API and then you upload a set of data on OutSystems side. If there is a single API that returns all the data you need, it would be better than calling an API that retrieves one record multiple times which would help performance and avoid those errors. 


Kind Regards,
João

Rank: #3075

Do you have any suggestion to save the API's data? I used the committransaction and it did save some data!
Thanks!

Rank: #56
Solution

Hi João,


I am glad I could help.

I don't know which API you are consuming to know if you have a more optimal approach than getting all the records 1-by-1. As I mentioned, usually this kind of imports are made by getting a subset of data from the API (as opposite to each record at a time) to avoid all these subsequent calls.


If you really need to do all of these calls and you see that you're getting the 403 due to a lot of calls consecutively, you may force a few seconds of wait between each API call (using for instance the Sleep forge component).


Kind Regards,
João