Correct way to use uuid generation #65
Replies: 4 comments 3 replies
-
We have changed tack slightly. Now we are not using the uuid library in postman and instead generating a uuid-like string by including this in the postman pre-request script instead of importing the third party library // const { v4: uuidv4 } = require('uuid');
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
pm.variables.set("myUuid", guid() ); This gets round the problem of then manually needing to import uuid to the flows.js after generation. |
Beta Was this translation helpful? Give feedback.
-
Hi David, It is a best practice to keep the usage of third party libraries to a minimum. Postman offers support for "dynamic" variables, which will allow you to generate all kind of variables, like an UUID by using {{$guid}} which will generate a uuid-v4 style guid. postman-to-k6 fully supports the available Postman dynamic variables, see https://github.com/apideck-libraries/postman-to-k6/blob/main/changes-since-1.5.0-release.md#full-support-of-the-postman-random-functionsdynamic-variables By using the build "dynamic" variables, there is no need to struggle with the usage of remote libraries. |
Beta Was this translation helpful? Give feedback.
-
Hi David, postman-to-k6 does support a large range of the external libraries: https://github.com/apideck-libraries/postman-to-k6/tree/main/lib/shim although we might need to review them. To come back on your use-case, you could still use a dynamic variable and have it generated upfront, using the collection "Pre-request scripts" or reqest "Pre-request script" const uniqueRef = pm.variables.replaceIn("{{$guid}}") // generates a GUID
pm.environment.set('uniqueRef', uniqueRef) // set the generated GUID as Postman env variable What would be the reason of using the |
Beta Was this translation helpful? Give feedback.
-
Oh interesting, replaceIn is not a syntax I've seen before. When you mentioned using {{$guid}} I was imagining setting the guid on the request json body as per... { "someGuid": "{{$guid}}" } ...Then having to read the So in fact the replaceIn method is something new for me and will help with some other solutions I've butchered previously! Thanks |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a postman collection in which one prerequest script generates a uuid using the uuid module to a variable and then uses this in further requests. For example:
When using postman-to-k6, this breaks and those requests are not sent by k6 because it does not find the uuid module. Following these instructions https://k6.io/docs/examples/generating-uuids/ and browserifying uuid then importing into the flows.js does not work as k6 throws the following error:
level=error msg="Error: crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported\n\tat
Based on the discussion here grafana/k6#725 the Crypto module is not supported by k6. After some further googling, this https://k6.io/docs/javascript-api/jslib/utils/uuidv4/ does work in my use case.
I therefore manually add the following at the top of the generated flows.js
My first question is whether I am using the correct approach and am I missing something? I'm also wondering whether this is potentially a bug with postman-to-k6 where uuid.js is not being added to the shims. There is a list of exclusions but I see no mention in the readme that the modules supported by Postman are not supported by postman-to-k6. I notice that uuid is a requirement of postman-to-k6 and so that module is available in the node_modules.
I realise there is some complexity because a module supported by Postman might not be directly supported by k6 however it would nevertheless be nice to not have to manually edit generated files (or write a script to do so) before execution if there is an overlap between the two tools. Ideally a compatible uuid.js would be available in the shims rather than having to download the k6-utils.
So I am not sure whether this is a bug or a feature request and whether my current approach is recommended in the mean time.
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions