We need to connect to a web service application using the web service connector. This web application requires a new random generated string (csrf token) to be included in the API URL for every call. For example, the call to do a Test Connection is “/api/v1/users?csrftoken=ABCD123”. The value of the “csrftoken” needs to be unique for every call.
So, the approach I’m thinking to do this will be to leverage on the Connector Rule for Web Service specifically using “Web Services Before Operation Rule” to perform the following:
Define string variable.
Use random generated number and store to string variable.
Include this generated string variable as part of the value for “csrftoken” in the API URL.
My question would be, since I’m not very familiar with Beanshell code, would like to ask:
Is the above approach viable?
Is there another better/simpler approach?
Any sample Beanshell script for the above requirement?
Can you let me know this random generated value is used for every API call or not.
if this random string required for every operation follow below steps:
1.Create a string
2.Generate a random string value.
3.Concat generated string value with API URL. using below code lines:
String csrftoken = "";
String contextUrl = "/api/v1/users?csrftoken="+csrftoken;
//add below statements in last line your code
requestEndPoint.setContextUrl(contextUrl);
return requestEndPoint;
I had a similar kind of requirement and i ended up using the EPOCH time as it will be always incrementing here’s a code snippet to get the time.
P.S if your API endpoint needs anything random and there is no need of alphanumeric string then you can use this to get a guranteed random string.
import java.time.Instant;
long exp = Instant.now().getEpochSecond();
String val = String.valueOf(exp); //convert to String
You can use this snippet to get the unique value always as it is based on time that is in seconds.
thank you so much for your guide~ that is a great advice on using EPOCH time~
For my case, since the limit is min and max of 12 characters, I’ve used the code guided by bapu. it works very well. i just need to include the definition for baseUrl.
I had applied the above beforeOperation rule to an “Account Aggregation” operation type. When calling this endpoint, this will return me with an array value of 2 items. However, when beforeOperation rule is applied, I am only getting back the first value of the array. I further tested by removing the beforeOperation rule, then I am able to get 2 values from the array.
Any idea what is causing this behaviour?
Its okay now. Mistake on the limit parameter.
Thanks!
EDIT: Changed the input to ISO8601, gives a better ouput
2nd EDIT: Since writing this, the input format now fails, and only accepts this “inputFormat”: “yyyy-MM-dd’T’HH:mm”, which means when doing many API calls quickly, they will all have the same number due to the lack of ‘seconds’, so might need to stick a random number on the end of it to make it unique.