I would like to generate a random token number something like UUID.randomUUID().
Is there a provision to generate or something java script equivalent.
Solved! Go to Solution.
EDIT
While the original answer here still *works*, there is a better, probably easier way. You can use AssignMessage.
<AssignMessage name='AM-1'> <AssignVariable> <Name>uuid</Name> <Template>{createUuid()}</Template> </AssignVariable> </AssignMessage>
original answer (2016)
Javascript can generate rfc4122-compliant guids. One example: https://gist.github.com/Nickdouille/8644841
I've done similar in a javascript policy without issue.
You could try something like this:
var transactionId = ""; transactionId = generateUUID(); function generateUUID(){ var d = new Date().getTime(); var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = (d + Math.random()*16)%16 | 0; d = Math.floor(d/16); return (c=='x' ? r : (r&0x7|0x8)).toString(16); }); return uuid; }
EDIT
While the original answer here still *works*, there is a better, probably easier way. You can use AssignMessage.
<AssignMessage name='AM-1'> <AssignVariable> <Name>uuid</Name> <Template>{createUuid()}</Template> </AssignVariable> </AssignMessage>
original answer (2016)
Javascript can generate rfc4122-compliant guids. One example: https://gist.github.com/Nickdouille/8644841
I've done similar in a javascript policy without issue.
I tried but everytime the variable gets assigned to NaN
Hi @Hemanth - can you show your code?
Thanks Dino, it is working. It was a simple mistake from my end.
You could try something like this:
var transactionId = ""; transactionId = generateUUID(); function generateUUID(){ var d = new Date().getTime(); var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = (d + Math.random()*16)%16 | 0; d = Math.floor(d/16); return (c=='x' ? r : (r&0x7|0x8)).toString(16); }); return uuid; }
can you pls explain the logic how below logic works:
function generateUUID(){var d =newDate().getTime();var uuid ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function(c){var r =(d +Math.random()*16)%16|0; d =Math.floor(d/16);return(c=='x'? r :(r&0x7|0x8)).toString(16);});
it's using the date as an input to derive random digits.
Each x in the source string is replaced with a random hex digit.
Just curious - what do you need the random number FOR ?
There is a messageid context variable which is unique to each inbound message, and maybe random enough. It may be suitable for your needs.
Well this is to generate a token, basically to handle cross talk issue.
@Meghdeep Basu @Hemanth
Instead of adding so much custom logic you can directly use Apigee Message Template which provides a createUuid function which can be plugged in AssignMessage policy to generate your Uuid without any custom logic
Reference :
https://docs.apigee.com/api-platform/reference/message-template-intro#create-uuid-function
This is a good comment... The createUuid is one of the functions that has been added since the time this Q was originally posted.
There is also a randomLong function available in the MessageTemplate as well.
example:
<AssignMessage name='AM-1'> <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> <AssignVariable> <Name>my_uuid</Name> <Template>{createUuid()}</Template> </AssignVariable> <AssignVariable> <Name>my_random</Name> <Template>{randomLong(1000,10000)}</Template> </AssignVariable> </AssignMessage>
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |